3

I'm referring to this post. I like the idea from 'a different ben' of the tabularx table inside the \Input command. But I would like to have the word Input: at the top, on the same line as the first row of the table. The suggested \newline in the linked post doesn't work for me.

MWE:

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

You have to use [t] for setting the vertical alignment; however, your tabular will be too wide.

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

enter image description here

1
  • Great, I didn't notice that the text will go over the right side, it was too short in my example. Jul 11 '14 at 14:38
5
\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}
3
  • Thanks :), I tried \begin{tabularx}[t]{\textwidth}{p{.03\textwidth}p{.03\textwidth}X} but that gave me dozen of errors. I didn't realize that the [t] needs to be after the second curly braces pair. Jul 11 '14 at 14:18
  • This does cause an overfull \hbox though...
    – Werner
    Jul 11 '14 at 14:19
  • @Werner yes I just answered the question, not the next one:-) Jul 11 '14 at 14:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.