1

I want to insert code snippets into a tabular environment. For the Code snippets I'm using the package listing and to insert it into a tabular environment I put the code into a box. To get linebreaks within my code I put all this in a minipage-environment. The top alignment (which is mandatory) of the cells with the code does not work properly anymore. I guess the problem is that I have to use something like an lrbox to insert the code into a tabular environment. Does anyone have an idea how to solve this problem? A general approach would be preferrable, because I want to use this regularly.

lstlisting-minipage-missaligned

edit: After some research I added a partial answer, but I still would appreciate some help with top-aligning.

\documentclass[%
    twoside=false,%
    paper=a4,%
    DIV=15,%)
    BCOR=0mm,%
    parskip=false,%
    fontsize=10pt,%
    ]{scrreprt}%
%%% TABLES
\usepackage{array}%
\usepackage{tabu}%
%\usepackage{tabularx}%
\usepackage{booktabs}%
%%% COLORS
\usepackage{color}% colors!
\usepackage{xcolor}%
\definecolor{graybg}{rgb}{0.95,0.95,0.95}%
%%% CODE HIGHLIGHTING
\usepackage{listings,lstautogobble}%
\lstset{        basicstyle=\small\ttfamily,
                breaklines=true,
                columns=[c]fixed,
                backgroundcolor=\color{graybg},
                numbersep=2pt,
                xleftmargin=0mm,
                xrightmargin=0mm,
                breakautoindent=false,
                autogobble=true
                }%
%
\newsavebox\mybox
\begin{document}%
%%%
\begin{lrbox}{\mybox}
\begin{minipage}[t]{0.33\textwidth}
 \begin{lstlisting}
   int main() {
   printf("hello, world");
   return 0;
   }
 \end{lstlisting}
 \end{minipage}
\end{lrbox}
%
{\tabulinesep=1.2mm%
\begin{table}[h!]%
\begin{tabu} to \textwidth {X[l,p]X[l,p]X[l,p]}%
\toprule%
\textbf{Description} & \textbf{Some code} & \textbf{Output}\tabularnewline%
\midrule%
some blabla & printf() & printf()\tabularnewline%   
hello world & \usebox\mybox & hello, world\tabularnewline%
\bottomrule%
\end{tabu}%
\end{table}%
}
%%%
\end{document}

1 Answer 1

0

If I use the package minted I am at least able to create code snippets inside the standard tabular environment, thanks to this link. Additionally this answer removes the extra vertical space successfully for top alignment (and calls this environment tabminted). tabminted-aligned-nobackgroundcoloring

\documentclass[%
    twoside=false,%
    paper=a4,%
    DIV=15,%)
    BCOR=0mm,%
    parskip=false,%
    fontsize=10pt,%
    ]{scrreprt}%
%%% TABLES
%\usepackage{array}%
%\usepackage{tabu}%
%\usepackage{tabularx}%
\usepackage{booktabs}%
%%% COLORS
\usepackage{color}%
\usepackage{xcolor}%
\definecolor{graybg}{rgb}{0.95,0.95,0.95}
%%% CODE HIGHLIGHTING
\usepackage[
    cache=false% cache=false longer compiling, but no cache errors
    ]{minted}
\setminted[c]{%
    autogobble=true,%
    breakanywhere=true,%
    breaklines=true,%
    breaksymbol=,%
    %bgcolor=graybg,% sadly not compatible
    breakanywheresymbolpre=%
    }%
%%% REDEFINE MINTED FOR BETTER SPACING IN TABLES
\makeatletter
\newenvironment{tabminted}{%
    \let\FV@ListVSpace\relax  
    \minted
}{%
    \endminted
    \unskip   
    \aftergroup\@tabmintedend
}
\newcommand*{\tabminted@finalstrut}[1]{%
    \ifdim\prevdepth>0pt
        \ifdim\dp#1>\prevdepth
            \vskip\dimexpr(\dp#1)-\prevdepth\relax
        \fi
    \else
        \vskip\dimexpr(\dp#1)\relax
    \fi
}
\newcommand*{\@tabmintedend}{%
    \let\@finalstrut\tabminted@finalstrut
}
\makeatother
%%% BEGIN DOCUMENT
\begin{document}%
%
\begin{table}[h!]%
\begin{tabular} {p{0.33\textwidth}p{0.33\textwidth}p{0.33\textwidth}}%
\toprule%
\textbf{Description} & \textbf{Some code} & \textbf{Output}\tabularnewline%
\midrule%
some blabla & printf() & printf()\tabularnewline%   
hello world &%
\begin{tabminted}{c}
int main() {
printf("hello, world");
return 0;
}
\end{tabminted}
& hello, world\tabularnewline%
\bottomrule%
\end{tabular}%
\end{table}%
%
\end{document}

If I do a minipage inside a tabular cell the top alignment gets messed up again. But here I am able to use background coloring again. Thanks to this I can align the minipage top perfectly - although this vspacealways feels hacky to me (I swapped out heavyrulewidth for lightrulewidth). minted-backgroundcolor-minipage

\documentclass[%
    twoside=false,%
    paper=a4,%
    DIV=15,%)
    BCOR=0mm,%
    parskip=false,%
    fontsize=10pt,%
    ]{scrreprt}%
%%% TABLES
%\usepackage{array}%
%\usepackage{tabu}%
%\usepackage{tabularx}%
\usepackage{booktabs}%
%%% COLORS
\usepackage{color}%
\usepackage{xcolor}%
\definecolor{graybg}{rgb}{0.95,0.95,0.95}
%%% CODE HIGHLIGHTING
\usepackage[
    cache=false%
    ]{minted}
\setminted[c]{%
    autogobble=true,%
    breakanywhere=true,%
    breaklines=true,%
    breaksymbol=,%
    bgcolor=graybg,%
    breakanywheresymbolpre=%
    }%
%%% BEGIN DOCUMENT
\begin{document}%
%
\begin{table}[h!]%
\begin{tabular} {p{0.33\textwidth}p{0.33\textwidth}p{0.33\textwidth}}%
\toprule%
\textbf{Description} & \textbf{Some code} & \textbf{Output}\tabularnewline%
\midrule%
some blabla & printf() & printf()\tabularnewline%   
hello world &%
\begin{minipage}[t]{0.33\textwidth}
\vspace*{-\dimexpr\baselineskip+\lightrulewidth+\abovetopsep\relax}
\begin{minted}{c}
int main() {
printf("hello, world");
return 0;
}
\end{minted}
\end{minipage}
& hello, world\tabularnewline%
\bottomrule%
\end{tabular}%
\end{table}%
%
\end{document}

To get something similar like tabulinesep I came across this. \renewcommand{\arraystretch}{1.5} does the trick.
If someone wants to use minted within a longtable-environment tabminted does not do the trick. You need to nest minted in a minipage to get the correct alignment. I added a picture to show the differences. longtable with minted and tabminted

\documentclass[%
    twoside=false,%
    paper=a4,%
    DIV=15,%)
    BCOR=0mm,%
    parskip=false,%
    fontsize=10pt,%
    ]{scrreprt}%
%%% TABLES
%\usepackage{array}%
%\usepackage{tabu}%
%\usepackage{tabularx}%
\usepackage{booktabs}%
\usepackage{longtable}%
%%% COLORS
\usepackage{color}%
\usepackage{xcolor}%
\definecolor{graybg}{rgb}{0.95,0.95,0.95}%
%%% CODE HIGHLIGHTING
\usepackage[
    cache=false
    ]{minted}
\setminted[c]{%
    autogobble=true,% 
    breakanywhere=true,% 
    breaklines=true,%
    breaksymbol=,% 
    %bgcolor=graybg,% not compatible without minipage
    breakanywheresymbolpre=% 
    }%
%%% REDEFINE MINTED FOR BETTER SPACING IN TABLES
\makeatletter
\newenvironment{tabminted}{%
    \let\FV@ListVSpace\relax  
    \minted
}{%
    \endminted
    \unskip   
    \aftergroup\@tabmintedend
}
\newcommand*{\tabminted@finalstrut}[1]{%
    \ifdim\prevdepth>0pt
        \ifdim\dp#1>\prevdepth
            \vskip\dimexpr(\dp#1)-\prevdepth\relax
        \fi
    \else
        \vskip\dimexpr(\dp#1)\relax
    \fi
}
\newcommand*{\@tabmintedend}{%
    \let\@finalstrut\tabminted@finalstrut
}
\makeatother
%%% BEGIN DOCUMENT
\begin{document}%
%
\renewcommand{\arraystretch}{1.5}
\begin{longtable}[l]{p{0.33\textwidth}p{0.33\textwidth}p{0.33\textwidth}}
%no caption nor label
\tabularnewline

\toprule
\textbf{Description} & \textbf{Some code} & \textbf{Output}\tabularnewline
\midrule
\endfirsthead

\toprule
\textbf{Description} & \textbf{Some code} & \textbf{Output}\tabularnewline
\midrule
\endhead

\bottomrule
\endfoot

\bottomrule
\endlastfoot

tabminted in longtable-environment & 
\begin{tabminted}{c}
int main() {
printf("hello, world");
return 0;
}
\end{tabminted}
&
line one\newline 
line two\newline
line three\newline 
line four\newline 
line five\newline 
line six\tabularnewline
minted-environment & 
\begin{minted}{c} 
int main() {
printf("hello, world");
return 0;
}
\end{minted}
&
line one\newline 
line two\newline
line three\newline 
line four\newline 
line five\newline 
line six\tabularnewline
minted- nested in minipage-environment & 
\begin{minipage}[t]{0.33\textwidth}
\begin{minted}{c} 
int main() {
printf("hello, world");
return 0;
}
\end{minted} 
\end{minipage}
&
line one\newline 
line two\newline
line three\newline 
line four\newline 
line five\newline 
line six\tabularnewline
\end{longtable}
%
\end{document}

You must log in to answer this question.

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