2

How can I create an environment that takes items in a way similar to lists?

Example:

\begin{myEnv}
\item{AAA}{BBB}
\item{CCC}{DDD}
\end{myEnv}

I'd like the above do something like

\begin{tabular}{|l|l|l|l|}
\hline
header1 & header2 & header3 & header4 \\ \hline
AAA & BBB & & \\ \hline
CCC & CCC & & \\ \hline
\end{tabular}

migrated from stackoverflow.com Feb 29 '12 at 16:33

This question came from our site for professional and enthusiast programmers.

4

Although I'm not entirely clear on your usage, the following is most likely what you're after. The given minimal working example shows your original tabular construction, followed by a myEnv construction. Each \item in within myEnv takes four arguments for the four columns. However, this can also be modified to only take two as in your example:

enter image description here

\documentclass{article}
\usepackage{booktabs}% http://ctan/org/pkg/booktabs
\newenvironment{myEnv}
  {\renewcommand{\item}[4]{##1&##2&##3&##4\\}% \item{<one>}{<two>}{<three>}{<four>}
   \tabular{*{4}{l}}
     \toprule
     header1 & header2 & header3 & header 4 \\
     \midrule
  }
  {\bottomrule\endtabular}
\begin{document}

\begin{tabular}{|l|l|l|l|}
\hline
header1 & header2 & header3 & header4 \\ \hline
AAA & BBB & & \\ \hline
CCC & CCC & & \\ \hline
\end{tabular}

\bigskip

\begin{myEnv}
  \item{AAA}{BBB}{CCC}{DDD}
  \item{EEE}{FFF}{GGG}{HHH}
\end{myEnv}

\end{document}

I took the liberty of using the booktabs package to produce the second myEnv table, since it produces professional-looking tables (without the use of vertical rules).

The redefinition of \item is local to the environment myEnv, and can therefore still be used in other list environments. Alternatively, you could make your own \item environment (say, \myItem) which would work only inside myEnv.

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.