Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question

2 Answers 2

up vote 3 down vote accepted

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

share|improve this answer
    
Great, I didn't notice that the text will go over the right side, it was too short in my example. –  user2653422 Jul 11 at 14:38
\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}
share|improve this answer
    
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. –  user2653422 Jul 11 at 14:18
    
This does cause an overfull \hbox though... –  Werner Jul 11 at 14:19
    
@Werner yes I just answered the question, not the next one:-) –  David Carlisle Jul 11 at 14:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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