GeneralHow to continue displaying an algorithm if it is too long?

LaTeX specific issues not fitting into one of the other forums of this category.
daiyue
Posts: 17
Joined: Mon May 21, 2012 5:58 pm

How to continue displaying an algorithm if it is too long?

Post by daiyue »

Hi, I have a situation that an algorithm has too many lines (too long), which can't be fitted into one page. Then the remainder of the algorithm couldn't be displayed in the next page so some reason. I am wondering how to resolve this issue?

ps. I am still using the same PhD thesis template (Cambridge version 2007), may be it is the cause of the problem?

cheers

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
User avatar
Stefan Kottwitz
Site Admin
Posts: 10290
Joined: Mon Mar 10, 2008 9:44 pm

How to continue displaying an algorithm if it is too long?

Post by Stefan Kottwitz »

Hello,

there are several packages for this purpose, such as algorithm, algorithm2e, algorithmic, algorithmicx, and more. We cannot now how you are doing it, so it's hard to guess what may be the issue for you. So, please post a Infominimal working example of your code, which shows the problem.

Stefan
LaTeX.org admin
daiyue
Posts: 17
Joined: Mon May 21, 2012 5:58 pm

How to continue displaying an algorithm if it is too long?

Post by daiyue »

Stefan_K wrote:Hello,

please post a Infominimal working example of your code, which shows the problem.

Stefan
Here it is.

Code: Select all

{\scriptsize
\begin{algorithm}
\caption{Construct relational tables}
\label{alg3}
\begin{algorithmic}[1]
\REQUIRE a set of data item clusters $C$
\ENSURE a relational table with all data items aligned
\STATE Set $tempItemSet$ and $currentRowItemSet$ to $\{\}$
\FOR{every cluster $c_i \in C$ for $i = 1, 2,..., n$}
    \STATE Sort the data items in $c_i$ from top to bottom
\ENDFOR
\STATE Sort the clusters in $C$ from left to right
\FOR{every cluster $c \in C$}
    \STATE Add $Item_i^1 \in c$ to $tempItemSet$ for $i = 1, 2,..., n$
\ENDFOR
\REPEAT
    \STATE Separate data items in $tempItemSet$ into groups \{$g_1, g_2,..., g_m$\}, based on their record numbers
    \IF{$g_i$ has the majority of data items in $tempItemSet$}
        \STATE Move data items in $g_i$ to $currentRowItemSet$
        \ELSE \IF{the first data item in $g_i$ has the left-most position among the first data items in the remaining groups}
                  \STATE Move data items in $g_i$ to $currentRowItemSet$
              \ENDIF
    \ENDIF
    \STATE Fill blanks into the positions left by the data items in $tempItemSet$ for $currentRowItemSet$
    \IF{there is a row above $currentRowItemSet$}
       \FOR{every remaining data item $i \in tempItemSet$ and $j \in currentRowItemSet$}
           \IF{$i$ matches any previous data item based on record number at its corresponding column}
              \STATE Add a new row under the least matched data item and put $i$ in its corresponding column
              \ELSE \IF{$i$ matches any previous data item based on record number at that row}
                        \STATE Put $i$ in its corresponding column of the row
                    \ENDIF
           \ENDIF
           \IF{$j$ matches any previous data item based on record number at that row}
               \STATE Put $j$ in its corresponding column of the row
           \ENDIF
       \ENDFOR
    \ENDIF
    \FOR{every cluster $c \in C$}
        \IF{$Item_i^j$ exists in $currentRowItemSet$ or $tempItemSet$ = \{\}}
           \STATE Add $Item_i^{j+1}$ to $tempItemSet$
        \ENDIF
    \ENDFOR
\UNTIL{$tempItemSet = \{\}$}
\end{algorithmic}
\end{algorithm}
}
I am using \usepackage{algorithm}.
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

How to continue displaying an algorithm if it is too long?

Post by cgnieder »

daiyue wrote:
Stefan_K wrote:please post a Infominimal working example of your code, which shows the problem.
Here it is.
This is no Infominimal working example. I guess you didn't follow the link? A minimal working example is a short but complete (!) document which can be compiled as is so anyone can reproduce the problem.

Regards
site moderator & package author
daiyue
Posts: 17
Joined: Mon May 21, 2012 5:58 pm

How to continue displaying an algorithm if it is too long?

Post by daiyue »

Sry for the minimal working example, as there a separated style class file, and a main tex file along with several individual tex files as chapters in the thesis. It might be difficult to pull up a minimal working example to reproduce the problem.

By Googling the algorithm split problem, I have started to use \algstore{myalg}. But it requires algorithmicx, so I pull up the following code in order to use it.

Code: Select all

\usepackage{algorithm}
\usepackage{algorithmicx}
\algnewcommand\algorithmicinput{\textbf{Input:}}
\algnewcommand\algorithmicoutput{\textbf{Output:}}
After I compiled the main tex file, I got the following error:

Code: Select all

! Undefined control sequence.
l.219 \REQUIRE
I was using

\usepackage[noend]{algorithmic}
\usepackage{algorithm}
\renewcommand{\algorithmicrequire}{\textbf{Input: }}
\renewcommand{\algorithmicensure}{\textbf{Output: }}


There was no problem (can refer to my last post for the algorithm).

How to resolve the issue?

Thanks
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

How to continue displaying an algorithm if it is too long?

Post by Johannes_B »

This seems to be a duplicate to TeX.SX.
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
User avatar
Stefan Kottwitz
Site Admin
Posts: 10290
Joined: Mon Mar 10, 2008 9:44 pm

How to continue displaying an algorithm if it is too long?

Post by Stefan Kottwitz »

Regarding your referencing question in the cross-post: you can place \label{...} and use \ref{...} as usual. Complete example, compile twice to get the ?? replaced:

Code: Select all

\documentclass{article}
\usepackage{dsfont}
\usepackage{algorithm}
\usepackage{algorithmicx}
\usepackage{algpseudocode}
\usepackage{mathtools}
\newcommand{\minbox}[2]{%
  \mathmakebox[\ifdim#1<\width\width\else#1\fi]{#2}}
\newcommand{\Let}[2]{\State $ \minbox{1em}{#1} \gets #2 $}
\algnewcommand{\Local}{\State\textbf{local variables: }}
\begin{document}
Note, the while loop ends at line \ref{endwhile}.
\begin{algorithm}
  \caption{Mandelbrot set}
  \label{alg:mandelbrot}
  \begin{algorithmic}[1]
    \Require{$c_x, c_y, \Sigma_{\max} \in \mathds{R},
      \quad i \in \mathds{N}, \quad i_{\max} > 0,
      \quad \Sigma_{\max} > 0$}
    \Function{mandelbrot}{$c_x, c_y, i_{\max},
              \Sigma_{\max}$}
      \Local{$x, y, x^\prime, y^\prime, i, \Sigma$}
      \Let{x, y, i, \Sigma}{0}
        \Comment{initial zero value for all}
      \While{$\Sigma \leq \Sigma_{\max}$
             and $i < i_{\max}$}
        \Let{x^\prime}{x^2 - y^2 + c_x}
        \Let{y^\prime}{2xy + c_y}
        \Let{m}{x^\prime}
        \Let{y}{y^\prime}
        \Let{\Sigma}{x^2 + y^2}
      \EndWhile\label{endwhile}
      \If{$i < i_{\max}$}
        \State \Return{$i$}
      \EndIf
      \State\Return{0}
    \EndFunction
  \end{algorithmic}
\end{algorithm}
\end{document}
\EndIf and such can hardly be shortened, ay they belong to the syntax. You need to close blocks.
LaTeX.org admin
daiyue
Posts: 17
Joined: Mon May 21, 2012 5:58 pm

How to continue displaying an algorithm if it is too long?

Post by daiyue »

Johannes_B wrote:This seems to be a duplicate to TeX.SX.
Sry, I didn't know the two sites are related. So I tried my luck and posted on both sites.
daiyue
Posts: 17
Joined: Mon May 21, 2012 5:58 pm

How to continue displaying an algorithm if it is too long?

Post by daiyue »

Stefan_K wrote: \EndIf and such can hardly be shortened, ay they belong to the syntax. You need to close blocks.
The thing is when I was using algorithmic, there seems to be no end if and such appears. So I was wondering how to turn such thing off.
User avatar
Stefan Kottwitz
Site Admin
Posts: 10290
Joined: Mon Mar 10, 2008 9:44 pm

How to continue displaying an algorithm if it is too long?

Post by Stefan Kottwitz »

Just post a link to the other site too, when you make a crosspost. Same for TeX StackExchange, add a link to the other post - I can say this also as a moderator there.

The link shall prevent double work, such as two people working out the same solution. So they would be informed.
daiyue wrote:Sry, I didn't know the two sites are related.
That's actually a benefit. So people might notice, and don't do work twice and waste time.

Completely unrelated: in that case people may blindly create similar solutions even when the issue is already solved on the other site. On an unrelated site they won't know. But a link will fix any crosspost-issue.

Stefan
LaTeX.org admin
Post Reply