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.

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

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

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

1 Answer 1

up vote 4 down vote accepted

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.

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.