6

I want an environment that creates a tabular inside a \parbox. The problem is how to put delimiters so that the whole thing becomes the second argument to the \parbox command.

\newenvironment{tabularinparbox}[1]{%
\parbox{\linewidth}
% ... what here ?
\begin{tabular}{#1}
}{%
\end{tabular}
% and here ?
}
Ernest A
  • 2,773

1 Answers1

11

You can create an environment as

\newenvironment{tabularinparbox}[1]{%
    \begin{minipage}{\linewidth}
       \begin{tabular}{#1}}
    {\end{tabular}%
     \end{minipage}}

Using \parbox itself won't work without loading some other packages. That's because you can't have unbalanced brackets in the beginning and ending instructions for \newenvironment. The minipage environment does essentially everything that \parbox does.

If you're particularly wed to \parbox then you can do something like the following:

\documentclass{article}
\usepackage{environ}
\NewEnviron{tabularinparbox}[1]{%
  \parbox{\linewidth}{%
    \begin{tabular}{#1}
      \BODY
    \end{tabular}%
   }}    
\begin{document}

\begin{tabularinparbox}{llcl}
this & that & a & sdlfkasjdf \\
a & b & asldfkjs d & slk
\end{tabularinparbox}

\end{document}

enter image description here

David Carlisle
  • 757,742
A.Ellett
  • 50,533