5

In the code below, everything is OK until I insert a blank line instead of one of the \\'s. Upon inserting a blank line, I'll get some errors. How can I get rid of these errors?

\documentclass{article}
\newsavebox\mybox
\newcommand{\Class}[1]
{ 
\sbox{\mybox}{\begin{tabular}{l}#1\end{tabular}}
\fbox{\parbox{\wd\mybox}{\texttt{#1}}}
}
\begin{document}
\Class
{double Width\\
 double Height\\
 double Perimeter\\ 
 double Area\\
}

\usebox\mybox
\end{document}

Edit:
My indented output is:
enter image description here

4
  • @Qrrbrbirlbel Any solutions which do the trick is appreciated. Feb 14, 2013 at 22:01
  • What do you intend this code to do, it s very weird, why are you setting #`` twice in both a tabular and a parbox? Surely it makes more sense just to put the \fbox` around the tabular? Feb 14, 2013 at 22:04
  • @DavidCarlisle Please see my update. I added a screenshot. Feb 14, 2013 at 22:13
  • 1
    "indented" or "intended"? Feb 15, 2013 at 0:22

2 Answers 2

7

The command \texttt does not allow paragraph breaks in its argument. You could use \ttfamily instead but a blank line inside a tabular doesn't do anything useful, so it is probably better to have the error message to warn of the incorrect input.

Perhaps this which makes a blank line do \\ ?

\documentclass{article}
\newsavebox\mybox
\usepackage{array}
\newcommand{\Class}[1]
{%
\sbox{\mybox}{\begin{tabular}{>{\expandafter\def\csname par\endcsname{\\}}l}#1\end{tabular}}%
\fbox{\parbox{\wd\mybox}{\par{\def\par{\\}\ttfamily{#1}}}}%
}
\begin{document}
\Class
{double Width\\
 double Height

 double Perimeter\\ 
 double Area\\
}

\usebox\mybox
\end{document}
4
  • So could you please provide me with another solution instead of the tabular environment which give me the same output? Feb 14, 2013 at 22:07
  • I could if I could guess what you want a blank line to do? As I say if you change \texttt to \ttfamily it works without error but perhaps isn't the output you want Feb 14, 2013 at 22:19
  • As for the output, You are completely right. If I change \texttt to \ttfamily, it works without errors, but the width of the frame, becomes bigger. Feb 14, 2013 at 22:27
  • @DavaidCarlisle your solution works, but could you please tell me why after inserting \Class {double Width\ double Height ‎bla‎ ‎bla‎‎ ‎bla‎‎ double Perimeter ‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎\\ double Area\}, the third bla falls in the next line in spite of the enough space? Feb 14, 2013 at 22:50
4

You can simply \fbox the tabular and use \\ for blank lines; to get typewriter type use \ttfamily; the \fbox keeps the font change local.

\documentclass{article}

\newcommand{\Class}[1]{%
  \fbox{\ttfamily\begin{tabular}{l}#1\end{tabular}}%
}

\begin{document}
\Class
{double Width\\
 double Height\\
 \\
 double Perimeter\\
 double Area\\
}

\end{document}

Beware of spurious spaces and comment end of lines in definitions.

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .