# Environment that generates table

I'm trying to define new nested environments that produce a 2x2 table. Each table cell in the second row will contain a source code.

What I tried to do-

Class file:

\newenvironment{example}
{
\begin{table}[]
\centering
\caption{Caption of Table}
\label{my-label}
\begin{tabular}{|l|l|}
\hline
Input & Output \\ \hline
}
{
\\ \hline
\end{tabular}
\end{table}
}

\newenvironment{inputExample}
{}{&}

\newenvironment{outputExample}
{}{}


TeX file:

\begin{example}
\begin{inputExample}
Left
column
content...
\end{inputExample}
\begin{outputExample}
Right column content...
\end{outputExample}
\end{example}


I don't know why, but it is not working. I got many error messages like-

'Missing } inserted. ...gin{inputExample}Left column content... \end{inputExample}'

If I remove the & from the definition of the input example environment, it works. But it obviously doesn't produce the correct table.

What I want is-

• Are these tables to show a LaTeX code on the left and its result on the right? If so, the showexpl package may be a better fit. – Mike Renfro Sep 1 '16 at 21:32
• Actually, it must show two source codes (like, one in C++ and the other one in Java). Anyways, I'd like to know why the cited errors happen. The structure seems to be OK. – Leandro Luque Sep 2 '16 at 12:14

This is just an initial proposal with tcolorbox. If you consider using it, some more work can be done to better adjust. This version uses tcbitem to include all four elements and all four boxes are similar, but all of them can be particularly designed.

\documentclass{article}
\usepackage[most]{tcolorbox}
\usepackage{lmodern}
\usepackage{lipsum}

\begin{document}

\lipsum[1]

\begin{table}
\caption{A nice table simulated with \texttt{tcolorbox}}
\begin{tcboxeditemize}[raster columns=2, colback=white, sharp corners, raster equal height=rows, raster force size=false, raster column 1/.style={add to width=-3cm}, raster column 2/.style={add to width=3cm}]{notitle,colback=white, sharp corners, boxsep=-2pt}
\tcbitem Input
\tcbitem Output
\tcbitem Left column content
\tcbitem Right column content
\end{tcboxeditemize}
\end{table}
\end{document}


EDIT:

It's possible to convert the tcboxeditemize in an simple environment:

\documentclass{article}
\usepackage[most]{tcolorbox}
\usepackage{lmodern}
\usepackage{lipsum}

\newenvironment{mybox}{%
\begin{tcboxeditemize}[raster columns=2, colback=white, sharp corners,
raster equal height=rows, raster force size=false,
{notitle,colback=white, sharp corners, boxsep=-2pt}}%
{\end{tcboxeditemize}}

\begin{document}

\lipsum[1]

\begin{table}
\caption{A nice table simulated with \texttt{tcolorbox}}
\begin{mybox}
\tcbitem Input
\tcbitem Output
\tcbitem Left column content
\tcbitem Right column content
\end{mybox}
\end{table}
\end{document}

• I'd like to do that with new environments. – Leandro Luque Sep 2 '16 at 12:15
• @LeandroLuque You can always declare a newenvironment to use tcboxeditemize. Please see my edit. – Ignasi Sep 5 '16 at 8:42
• Can you explain why the original version did not work? – Martin Nyolt Sep 5 '16 at 8:46
• Exemple above doesn't compile with overleaf.com (error produced :LaTeX Error: Environment tcboxeditemize undefined.) Is there a way to fix easily? – pzorba75 Sep 5 '16 at 9:20
• @pzorba75 Because overleaf use an old tcolorbox version. I think you can download a recent package version in your project folder. – Ignasi Sep 5 '16 at 9:47