2

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-

table

2
  • 1
    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. Commented Sep 1, 2016 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. Commented Sep 2, 2016 at 12:14

1 Answer 1

2

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}

enter image description here

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, 
    raster column 1/.style={add to width=-3cm}, 
    raster column 2/.style={add to width=3cm}]%
    {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}
6
  • I'd like to do that with new environments. Commented Sep 2, 2016 at 12:15
  • @LeandroLuque You can always declare a newenvironment to use tcboxeditemize. Please see my edit.
    – Ignasi
    Commented Sep 5, 2016 at 8:42
  • 1
    Can you explain why the original version did not work? Commented Sep 5, 2016 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
    Commented Sep 5, 2016 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
    Commented Sep 5, 2016 at 9:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .