2

the following algorithm exceeded the width of the column:

\begin{algorithm}[!htb]
\caption{........}
\label{alg:BuildMG} 
%\algsetup{linenosize=\small}

\scriptsize
\DontPrintSemicolon

\SetKwInOut{Input}{input}\SetKwInOut{Output}{output}
\Input{$Path, CV\_LOAD\_IMAGE\_COLOR$\tcc*[f] { ......}\\} 
\Output{$Array~of~Roots$ \tcc*[f] { .........}\\}

\Begin{
    \nl $InvestigateDirSubDir(Path)$\\
    \nl\ForEach{$file$  \tcc*[f] { ............}\\}{
        \nl\If{$Matobject.data$}{       
             \nl $Increment~~NumberOfImages$ \\
             \nl $cvtColor(-,-,CV\_BGR2GRAY)$\\
             \nl $getHeightWidth()$\\            
             \nl $HeadDLL = buildAgrid(height, width)$\\
             \nl $Previous=arrayOfRoots[NumberOfImages\%NumberOfGroups].east$
             \nl $arrayOfRoots[NumberOfImages\%NumberOfGroups].east=HeadDLL$\\          
             \nl $TailDLL = getLastgrid(HeadDLL)$\\
             \nl $TailDLL->south = Previous$\\           
             }           
        }
    }


\end{algorithm}

and the result was as in the following image :

enter image description here

  • 2
    An MWE would be very helpful here, especially since you've tagged three algorithm-related packages. I assume the width problem is partially due to this being a two-column document? – Mike Renfro Mar 06 '16 at 17:09
  • 3
    You really really shouldn't use $ ... $ for italicised text, $ ... $ is for mathematics and so letters are treated as variables and spaced as such. So Of within $ ... $ is not treated as the word "Of" but some variable O multiplied by some variable f, which is why you have that horrible spacing between O and f in arrayOfRoots (to give one example). This spacing is horrible within an italicised word, but quite right if these are mathematical variables being multiplied – Au101 Mar 06 '16 at 17:38
  • 2
    just using `\mathit{...} for the "words" will decrease the horizontal space required. this may be enough so that the lines will fit the available width. – barbara beeton Mar 06 '16 at 17:56
  • @Au101 thank u so much for your answer, its working :) – Student. Engineering Mar 07 '16 at 07:42

1 Answers1

4

Based on your algorithm variables there is not much you can do other than:

  • shortening the variables names (while still maintaining readability):
    Perhaps use ImageNum and GroupNum instead of NumberOfImages and NumberOfGroups.

  • reducing the horizontal indentation for nested structures:
    Use \SetInd to reduce the space around the vertical lines.

The line-breaking is as expected, since there is no hyphenation pattern specified for the "words" used in your algorithm. Moreover, setting everything inside math mode allows break-points only to occur at specific locations, like binary operators (+, say) or relations (=, say).

The below example attempts to highlight both the above measures, as well as suggest a more appropriate formatting for the components of your algorithm. To that end, it defines

  • \var{<variable>} to set a <variable>;

  • \proc{<procedure>}{<arguments>} to set a <procedure> with possible <arguments>; and

  • \prop{<property>} to set a <property> associated with a variable:

Defining macros to take care of the formatting allows you to easily change this in the future.

enter image description here

\documentclass{article}

\usepackage[ruled,vlined]{algorithm2e}

\newcommand{\var}{\texttt}
\newcommand{\proc}[2]{\textsl{#1}(#2)}
\newcommand{\prop}{\textit}

\begin{document}

\begin{algorithm}[H]
  \caption{Building MedGraph}

  \footnotesize
  \DontPrintSemicolon

  \SetKwInOut{Input}{input}\SetKwInOut{Output}{output}
  \Input{\var{Path}, \var{CV\_LOAD\_IMAGE\_COLOR} \tcc*[f] {Path to the directory and subdirectories that contain images}}
  \Output{Array of roots \tcc*[f] {The indexing structure}}

  \Begin{
    \nl \proc{InvestigateDirSubDir}{\var{Path}}\;
    \tcc*[f] { Mat object is used as a handler for a file} \\[-\baselineskip]
    \nl\ForEach{\var{file}}{
      \nl\If{\var{Matobject.data}}{       
        \nl Increment \var{ImageNum}\;
        \nl \proc{cvtColor}{\var{-},\var{-},\var{CV\_BGR2GRAY)}}\;
        \nl \proc{getHeightWidth}{}\;
        \nl $\var{HeadDLL} = \proc{buildAgrid}{\var{height}, \var{width}}$\;
        \nl $\var{Previous} = \var{arrayOfRoots}[\var{ImageNum\%GroupNum}].\prop{east}$\;
        \nl $\var{arrayOfRoots}[\var{ImageNum\%GroupNum}].\prop{east} = \var{HeadDLL}$\;
        \nl $\var{TailDLL} = \proc{getLastgrid}{\var{HeadDLL}}$\;
        \nl $\var{TailDLL}.\prop{south} = \var{Previous}$\;
      }
    }
  }
\end{algorithm}

%\SetInd{<before rule space>}{<after rule space>}
%\SetInd{0.5em}{1em}% Default
\SetInd{0.5em}{0.5em}

\begin{algorithm}[H]
  \caption{Building MedGraph}

  \footnotesize
  \DontPrintSemicolon

  \SetKwInOut{Input}{input}\SetKwInOut{Output}{output}
  \Input{\var{Path}, \var{CV\_LOAD\_IMAGE\_COLOR} \tcc*[f] {Path to the directory and subdirectories that contain images}}
  \Output{Array of roots \tcc*[f] {The indexing structure}}

  \Begin{
    \nl \proc{InvestigateDirSubDir}{\var{Path}}\;
    \tcc*[f] { Mat object is used as a handler for a file} \\[-\baselineskip]
    \nl\ForEach{\var{file}}{
      \nl\If{\var{Matobject.data}}{       
        \nl Increment \var{ImageNum}\;
        \nl \proc{cvtColor}{\var{-},\var{-},\var{CV\_BGR2GRAY)}}\;
        \nl \proc{getHeightWidth}{}\;
        \nl $\var{HeadDLL} = \proc{buildAgrid}{\var{height}, \var{width}}$\;
        \nl $\var{Previous} = \var{arrayOfRoots}[\var{ImageNum\%GroupNum}].\prop{east}$\;
        \nl $\var{arrayOfRoots}[\var{ImageNum\%GroupNum}].\prop{east} = \var{HeadDLL}$\;
        \nl $\var{TailDLL} = \proc{getLastgrid}{\var{HeadDLL}}$\;
        \nl $\var{TailDLL}.\prop{south} = \var{Previous}$\;
      }
    }
  }
\end{algorithm}

\end{document}

\SetInd{<before rule space>}{<after rule space>} was used to adjust the spacing around the vertical rules in the second algorithm.

Another option would be to reduce the font size even more, but that's a bit ridiculous. Finally, you can set the entire algorithm in a box and shrink it to fit the \linewidth. However, that's equivalently non-beautiful.

Werner
  • 603,163