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.

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

share|improve this question
    
@Qrrbrbirlbel Any solutions which do the trick is appreciated. –  Vahid Damanafshan Feb 14 '13 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? –  David Carlisle Feb 14 '13 at 22:04
    
@DavidCarlisle Please see my update. I added a screenshot. –  Vahid Damanafshan Feb 14 '13 at 22:13
    
"indented" or "intended"? –  stalking is prohibited Feb 15 '13 at 0:22

2 Answers 2

up vote 6 down vote accepted

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}
share|improve this answer
    
So could you please provide me with another solution instead of the tabular environment which give me the same output? –  Vahid Damanafshan Feb 14 '13 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 –  David Carlisle Feb 14 '13 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. –  Vahid Damanafshan Feb 14 '13 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? –  Vahid Damanafshan Feb 14 '13 at 22:50

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

share|improve this answer

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.