如何在乳胶中将算法与线宽相匹配

如何在乳胶中将算法与线宽相匹配

以下算法超出了列的宽度:

\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}

结果如下图所示:

在此处输入图片描述

答案1

根据您的算法变量,除了以下之外您无能为力:

  • 缩短变量名称(同时保持可读性):
    也许使用ImageNumandGroupNum而不是NumberOfImagesand NumberOfGroups

  • 减少嵌套结构的水平缩进:
    用于减少垂直\SetInd周围的空间。vlines

换行符合预期,因为您的算法中使用的“单词”没有指定连字符模式。此外,在数学模式中设置所有内容允许断点仅出现在特定位置,例如二元运算符(+例如)或关系(=例如)。

下面的示例试图强调上述两种措施,并为算法的组件建议更合适的格式。为此,它定义了

  • \var{<variable>}设置一个<variable>

  • \proc{<procedure>}{<arguments>}设置一个<procedure>可能的<arguments>;并且

  • \prop{<property>}设置<property>与变量关联的:

定义宏来处理格式允许您在将来轻松更改此设置。

在此处输入图片描述

\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>}用于调整第二种算法中v垂直线周围的间距。rules

另一个选择是进一步减小字体大小,但这有点荒谬。最后,您可以将整个算法放在一个框中,然后将其缩小以适合\linewidth。但是,这同样不美观。

相关内容