3

Suppose a) you are preparing a LaTeX manual and you wanted to exemplify a table and its source code side by side and b) you thought about using tcolorbox because of its features for handling similar situations (source code and its compiled result side by side or on top of each other etc.) with its listings library.

The following works fine:

\documentclass{article}
\usepackage{tcolorbox}
\tcbuselibrary{listings,skins,xparse}


\begin{document}

\begin{tcblisting}{}

    \begin{tabular}{lll}
        \hline
        texto & texto & texto\\
        \hline
        texto & texto & texto\\
        \hline
    \end{tabular}
\end{tcblisting}
\end{document}

However, if I wrap tabular inside a table environment, the compiled result doesn't show up (only the code):

\begin{tcblisting}{}
    \begin{table}
    \begin{tabular}{lll}
        \hline
        texto & texto & texto\\
        \hline
        texto & texto & texto\\
        \hline
    \end{tabular}
    \end{table}
\end{tcblisting}

Of course my goal is not letting the table float: my goal is just to optimize space and appearance by showing the aforementioned elementary table example side by side with its visual output inside a tcolorbox, as one can do with showexpl:

\documentclass{article}
\usepackage[utf8]{inputenc}
\setlength{\belowcaptionskip}{2pt}
\usepackage{showexpl}

\begin{document}


\begin{LTXexample} 
 \begin{table}
 \caption{A table}
 \centering 
\begin{tabular}{lll}
\hline        
texto & texto & texto\\
\hline
texto & texto & texto\\
\hlin
\end{tabular}
\end{table}
\end{LTXexample}

\end{document}

enter image description here

Is there a way to make this work with tcolorbox? If yes, how?

9
  • 2
    This won't work if you want to show also the compilation. You need then to prepare the output externally. With \begin{tcblisting}{listing only} it works fine. Commented Dec 4, 2017 at 21:38
  • What is the purpose of letting the table float away if you want to display both code and output?
    – user31729
    Commented Dec 5, 2017 at 6:37
  • @ChristianHupfer, actually just to show the result of the code side by side instead of using minipages or showing the code and then the actual table afterwards.
    – user9424
    Commented Dec 5, 2017 at 10:28
  • @Joseph: Yes, but this is pretty contradicting to use the table environment then
    – user31729
    Commented Dec 5, 2017 at 11:59
  • 1
    @Joseph: I explicitly wrote table, not the tabular environment.
    – user31729
    Commented Dec 5, 2017 at 17:02

2 Answers 2

2

Here are two options:

  • disable floating by emptying the table environment locally. This is okay, as long as the environment does not have a caption (simply redefining \caption to \captionof from the caption package does not work).
  • using the above* key which allows even floating material, but beware of floating tables (it might not stay at the listing, but float away).

non floating table

Code:

\documentclass{article}
\usepackage{tcolorbox}
\tcbuselibrary{listings,skins,xparse}

\begin{document}

\begin{tcblisting}{listing side text, before lower={%
    \renewenvironment{table}{}{}%
}}
\begin{table}
    \begin{tabular}{lll}
        \hline
        texto & texto & texto\\
        \hline
        texto & texto & texto\\
        \hline
    \end{tabular}
\end{table}
\end{tcblisting}
\begin{tcblisting}{listing above* text}
\begin{table}[ht]
      \centering
    \begin{tabular}{lll}
        \hline
        texto & texto & texto\\
        \hline
        texto & texto & texto\\
        \hline
    \end{tabular}
    \caption{Quack}
\end{table}
\end{tcblisting}
\end{document}
2

Another solution with tcolorbox. See section 15.7 Option Keys for Processing and Full Document Examples.

A compilable document with a table is included into a tcblisting which will be processed during main document processing. This will create an external document which will be processed and its pdf result included as comment.

As we are only interested in table part of our external listing, we can select these lines with firstline and lastline options.

And as we are only interested in table, we need to crop the resulting document. This is done with run system command option. It's possible to use the automatically assigned names to external files, but I could resolve it, so I used the known name. In this case my file was sensetitol-1.tex and tcolorbox creates sensetitol-1-listing-1.tex. This was the name used as input and output parameter for crop command.

Update: Thomas F. Stürm help me to solve the problem with the automatic names for external listings. with updated code there's no need to know the name of working file.

\documentclass[a4paper]{article}
\usepackage{geometry}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[most]{tcolorbox}

\makeatletter
\tcbset{%
    run pdfcrop/.style={%
        run system command={%
            pdfcrop\space\filename@area\[email protected]\space\filename@area\[email protected]}},  
}
\makeatother

\begin{document}

\begin{tcblisting}{%
    enhanced jigsaw, lower separated=false,
    leftlower=0pt, rightlower=0pt,
    colframe=red!50!black, colback=yellow!10!white,
    listing options={%
        style=tcblatex, texcsstyle=*\color{red!70!black}, 
        firstline=8, lastline=18},
    listing side comment,
    pdf comment,
    %freeze pdf,
    compilable listing,
    run pdflatex,
    run pdfcrop,
    %run system command=pdfcrop sensetitol-1-listing-1.pdf sensetitol-1-listing-1.pdf,
}
\documentclass[a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{geometry}
\begin{document}
\thispagestyle{empty}
\begin{table}
\caption{A table}
\centering 
\begin{tabular}{lll}
\hline        
texto & texto & texto\\
\hline
texto & texto & texto\\
\hline
\end{tabular}
\end{table}
\end{document}
\end{tcblisting}
\end{document}

enter image description here

You must log in to answer this question.