我正在尝试编写一个 LaTeX 设置来重现a2ps
显示源代码的方式。以下是此类布局的示例:
目前,我尝试使用 tcolorbox 来构建这样的布局。这是我做的一个简化的尝试:
\documentclass[a4paper,7pt,landscape,twocolumn]{article}
%% Build with: pdflatex -shell-escape example.tex
%% Font packages
\usepackage{lmodern}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
%% Minted
\usepackage{minted}
\usepackage[many]{tcolorbox}
\tcbuselibrary{listings}
\tcbuselibrary{minted}
%% Display the file 'filename' (possibly on several pages) and
%% highlight the syntax for the programming language 'language' as for
%% minted. The filename displayed can optionally be changed.
%%
%% \codeinput[displayed_name]{filename}{language}
%%
\newtcbinputlisting{\codeinput}[3][]{%
title={\large\bfseries\tt%
\if\relax\detokenize{#1}\relax{#2}\else{#1}\fi},
listing engine=minted,
minted options={autogobble,breaklines},
minted language=#3,
listing file={#2},
listing only,
size=title,
breakable,
height fill,
enhanced,
overlay first={\draw[line width=.75pt](frame.south west)--(frame.south east);},
overlay middle={\draw[line width=.75pt](frame.south west)--(frame.south east);%
\draw[line width=.75pt](frame.north west)--(frame.north east);},
overlay last={\draw[line width=.75pt](frame.north west)--(frame.north east);},
colframe=black,
colbacktitle=lightgray!85,
coltitle=black,
boxrule=.75pt,
colback=white,
coltext=black
}
\begin{document}
%% Display the source file
\codeinput[libtrie/src/trie.c]{./trie.c}{c}
\end{document}
并且,编译后我得到以下结果:
我有两个问题:
我们如何才能在每个新分页符的顶部重复框架标题?
一个小的视觉错误是可拆分页面的第一页的高度略小于后面的页面。如果有人知道它来自哪里以及如何修复它,那就太好了!
答案1
我终于找到了答案(花了我很长时间,所以我将在这里解释它以节省其他人的时间)。
因此,要考虑的正确选项是title after break=<Title>
。事实上,它确实达到了预期的效果。如果我们以最初的最小示例为例:
\documentclass[a4paper,landscape,twocolumn]{article}
%% Build with: pdflatex -shell-escape example.tex
%% Font packages
\usepackage{lmodern}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
%% Minted
\usepackage{minted}
\usepackage[many]{tcolorbox}
\tcbuselibrary{listings}
\tcbuselibrary{minted}
%% Display the file 'filename' (possibly on several pages) and
%% highlight the syntax for the programming language 'language' as for
%% minted. The filename displayed can optionally be changed.
%%
%% \codeinput[displayed_name]{filename}{language}
%%
\newtcbinputlisting{\codeinput}[3][]{%
title={\large\bfseries\tt%
\if\relax\detokenize{#1}\relax{#2}\else{#1}\fi},
title after break={\large\bfseries\tt%
\if\relax\detokenize{#1}\relax{#2}\else{#1}\fi},
listing engine=minted,
minted options={autogobble,breaklines},
minted language=#3,
listing file={#2},
listing only,
size=title,
breakable,
height fill,
enhanced,
overlay first={\draw[line width=.75pt](frame.south west)--(frame.south east);},
overlay middle={\draw[line width=.75pt](frame.south west)--(frame.south east);%
\draw[line width=.75pt](frame.north west)--(frame.north east);},
overlay last={\draw[line width=.75pt](frame.north west)--(frame.north east);},
colframe=black,
colbacktitle=lightgray!85,
coltitle=black,
boxrule=.75pt,
colback=white,
coltext=black
}
\begin{document}
%% Display the source file
\codeinput[libtrie/src/trie.c]{./trie.c}{c}
\end{document}
正如预期,我们得到:
还要注意,第一页和第二页之间的微小偏移也消失了。