将 algorithm2e 的单词输入与 tabularx 表对齐

将 algorithm2e 的单词输入与 tabularx 表对齐

我指的是这个邮政。我喜欢命令中 tabularx 表格的“a different ben”的想法\Input。但我希望将单词放在Input:顶部,与表格的第一行在同一行。\newline链接帖子中的建议对我来说不起作用。

梅威瑟:

\documentclass{scrbook}
\usepackage{tabularx}
\usepackage[ruled,longend,german,algochapter]{algorithm2e}
\usepackage{lipsum}


\begin{document}
  \begin{algorithm}[tbp]
    \SetKwInOut{Input}{Input}
    \SetKwInOut{Output}{Output}
    \LinesNumbered
    \caption[Description]{Description}
\Input{\begin{tabularx}{\textwidth}{p{.03\textwidth}p{.03\textwidth}X}
                    $a$ &   -- &    Desc1   \\
                    $b$ &   -- &    Desc2   \\
                    $c$ &   -- &    Desc3   
        \end{tabularx}
      }
  \end{algorithm}
\end{document}

答案1

您必须使用[t]来设置垂直对齐;但是,您的表格将太宽。

\documentclass[draft]{scrbook}

\usepackage[pass,showframe]{geometry} % just to show the margins

\usepackage{tabularx,calc}
\usepackage[ruled,longend,german,algochapter]{algorithm2e}
\usepackage{lipsum}

\newenvironment{algotabularx}
 {\tabularx{\linewidth-\inoutsize-\widthof{~~~}-4\tabcolsep-\rightskip}[t]}
 {\endtabularx}


\begin{document}

\begin{algorithm}[tbp]
\SetKwInOut{Input}{Input}
\SetKwInOut{Output}{Output}
\LinesNumbered
\caption[Description]{Description}
\Input{Something to show the alignment}
\Input{%
  \begin{algotabularx}{@{}p{.03\textwidth}p{.03\textwidth}X@{}}
  $a$ &   -- &    \lipsum*[2]  \\
  $b$ &   -- &    Desc2   \\
  $c$ &   -- &    Desc3   
  \end{algotabularx}%
}
\Output{Something else}
\end{algorithm}
\end{document}

在此处输入图片描述

答案2

\documentclass{scrbook}
\usepackage{tabularx}
\usepackage[ruled,longend,german,algochapter]{algorithm2e}
\usepackage{lipsum}


\begin{document}
  \begin{algorithm}[tbp]
    \SetKwInOut{Input}{Input}
    \SetKwInOut{Output}{Output}
    \LinesNumbered
    \caption[Description]{Description}
\Input{\begin{tabularx}{\textwidth}[t]{p{.03\textwidth}p{.03\textwidth}X}
                                   %%%
                                   %%%
                                   %%%
                    $a$ &   -- &    Desc1   \\
                    $b$ &   -- &    Desc2   \\
                    $c$ &   -- &    Desc3   
        \end{tabularx}
      }
  \end{algorithm}
\end{document}

相关内容