我想将代码片段插入表格环境。对于代码片段,我使用包列表,为了将其插入表格环境,我将代码放入一个框中。为了在我的代码中获得换行符,我将所有这些都放在一个迷你页面环境中。顶部对齐(这是强制性的)带有代码的单元格不再正常工作。我猜问题是我必须使用类似 lrbox 的东西将代码插入表格环境。有人知道如何解决这个问题吗?最好采用通用方法,因为我想经常使用它。
编辑:经过一番研究,我添加了部分答案,但我仍然希望得到一些有关顶部对齐的帮助。
\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
如果我使用包铸造我至少能够在标准tabular
环境中创建代码片段,这要归功于这链接。另外这答案成功删除了顶部对齐的额外垂直空间(并调用此环境tabminted
)。
\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}
如果我minipage
在表格单元格内执行此操作,顶部对齐会再次混乱。但在这里我可以再次使用背景颜色。感谢这我可以完美地对齐迷你页面顶部 - 虽然这vspace
总是感觉黑客对我来说(我换成了heavyrulewidth
)lightrulewidth
。
\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}
为了得到类似的东西,tabulinesep
我遇到了这.\renewcommand{\arraystretch}{1.5}
就可以了。
如果有人想在longtable
-environment中使用 minted tabminted
,不是成功了。您需要嵌套minted
才能minipage
获得正确的对齐。我添加了一张图片来显示差异。
\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}