我想使用 LaTeX 来记录我的 C++ 代码,因为我能够轻松排版数学,以及使用 TikZ 创建图表。我制作了一个最小的 LaTeX 模板进行测试,但我已经遇到了一些我想修复的问题:
- 转义的 LaTeX 代码中,每行文本的末尾不会生成空格(如果您查看下面的渲染输出,您应该会看到一些单词粘在一起)。
- 我希望转义的 LaTeX 代码中的环境保持在阴影边界内,行号位于每行的左侧(其余代码也是如此)。特别是,我希望强制源代码行号与其在 PDF 中的渲染输出之间保持 1:1 的对应关系。
您能建议如何解决这些问题吗?理想情况下,我还希望能够在文档中包含 TikZ 图片,这样图表占用的代码行数与我用来创建它的转义 LaTeX 代码的长度完全相同。但如果这太难实现,那么没有它我也可以接受。
非常感谢您的宝贵时间!
渲染输出
LaTeX 代码
\documentclass[11pt, a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage[top=0.4in,bottom=0in,left=0.7in,right=0.7in]{geometry}
\usepackage{listings}
\usepackage{mdframed}
\usepackage[usenames,dvipsnames]{xcolor}
\pagestyle{empty}
\lstset{
language=C++,
basicstyle=\sffamily,
commentstyle=\color{white!40!black},
escapechar=`,
escapebegin=\color{white!40!black},
escapeend={},
keywordstyle=\bfseries\color{Periwinkle},
numbers=left,
numberstyle=\sffamily\tiny\color{gray},
showstringspaces=false
}
\begin{document}
\begin{mdframed}[backgroundcolor=yellow!20,linewidth=0pt,
innertopmargin=0pt,innerbottommargin=0pt]
{\footnotesize
\lstinputlisting{test.hpp}
}
\end{mdframed}
\end{document}
示例 C++ 头文件
/*
** File Name: test.hpp
** Date: 04/25/2012
*/
#ifndef TEST_HPP_INCLUDED__
#define TEST_HPP_INCLUDED__
/*
** Template Parameters:
**
`\begin{enumerate}
\item[Schedule] A forward iterator that traverses the edge ordering that
ought to be used for each iteration.
\item[EdgeToFactorMap] A map of edges to factors. We can always transform a
graph such that the node potentials are subsumed into the edge potentials, so
this constraint does not result in a loss of generality.
\item[MessageFunc] A function used to compute the message
$\delta_{i \rightarrow j}$. It should accept as parameters the following:
\begin{itemize}
\item A const reference to a factor along $E_{ij}$.
\item A reference to the target factor to be written to.
\item The starting and ending iterators to a container of factors
representing the messages received by the sending clique, $C_{i}$.
\item The starting and ending iterators to a container of factors
representing the messages received by the receiving clique, $C_{j}$.
\end{itemize}
\item[Allocator] The allocator that ought to be used to obtain temporary
memory for the algorithm.
\end{enumerate}`
**
*/
template <
class Schedule,
class EdgeToFactorMap,
class MessageFunc,
class Allocator
>
#endif
答案1
我不太了解这个listings
包,所以可能有一个更高级别的接口,但我会
- 使用
description
而不是,enumerate
这样 laTex 就可以预期更宽的标签,而不会将它们粘贴在左边距 - 定义行尾字符以吞噬两个
**
(如果存在)并增加行计数器。(实际上,如果您同时允许正常的乳胶排版,则排版行号会更难) - 我也注释掉了,
adforn
因为我没有它。
所以我得到:
代码文件:
/*
** File Name: test.hpp
** Date: 04/25/2012
*/
#ifndef TEST_HPP_INCLUDED__
#define TEST_HPP_INCLUDED__
/*
** Template Parameters:
**
** `\begin{description}
** \item[Schedule] A forward iterator that traverses the edge ordering that
** ought to be used for each iteration.
** \item[EdgeToFactorMap] A map of edges to factors. We can always transform a
** graph such that the node potentials are subsumed into the edge potentials, so
** this constraint does not result in a loss of generality.
** \item[MessageFunc] A function used to compute the message
** $\delta_{i \rightarrow j}$. It should accept as parameters the following:
** \begin{itemize}
** \item A const reference to a factor along $E_{ij}$.
** \item A reference to the target factor to be written to.
** \item The starting and ending iterators to a container of factors
** representing the messages received by the sending clique, $C_{i}$.
** \item The starting and ending iterators to a container of factors
** representing the messages received by the receiving clique, $C_{j}$.
** \end{itemize}
** \item[Allocator] The allocator that ought to be used to obtain temporary
** memory for the algorithm.
** \end{description}`
**
*/
template <
class Schedule,
class EdgeToFactorMap,
class MessageFunc,
class Allocator
>
#endif
TeX 文件:
\documentclass[11pt, a4paper]{article}
%\usepackage{adforn}
\usepackage[T1]{fontenc}
\usepackage[top=0.4in,bottom=0in,left=0.7in,right=0.7in]{geometry}
\usepackage{listings}
\usepackage{mdframed}
\usepackage[usenames,dvipsnames]{xcolor}
\pagestyle{empty}
\def\sstar{**}
\begingroup
\makeatletter
\endlinechar-1
\catcode`\^^M\active
\gdef\foo{%
\let\savedM^^M%
\def^^M##1##2{%
\global\advance\lst@lineno\@ne
\global\advance\c@lstnumber\@ne
\def\tmpa{##1##2}%
\ifx\tmpa\sstar
\expandafter\@gobbletwo
\fi
##1##2}%
}
\endgroup
\lstset{
language=C++,
basicstyle=\sffamily,
commentstyle=\color{white!40!black},
escapechar=`,
escapebegin=\color{white!40!black}\foo,
escapeend={},
keywordstyle=\bfseries\color{Periwinkle},
numbers=left,
numberstyle=\sffamily\tiny\color{gray},
showstringspaces=false
}
\begin{document}
\begin{mdframed}[backgroundcolor=yellow!20,linewidth=0pt,
innertopmargin=0pt,innerbottommargin=0pt]
{\footnotesize
\lstinputlisting{test.hpp}
}
\end{mdframed}
\end{document}
答案2
我有一些部分解决方案,所以我会在想出解决方案时发布它们。在简单情况下,可以通过附加以下内容来解决缩进问题
\def\leftoff{1.5in}
\setlength{\leftmargini}{\leftmargini+\leftoff}
\setlength{\leftmarginii}{\leftmarginii+\leftoff}
到序言。这修复了简单的用例,但在环境enumerate
嵌套在另一个环境中的情况下添加了多余的缩进。因此,我建议您不要enumerate
在源代码中使用环境(表格内部除外),而是将环境用作tabular
最外层的容器。这样可以更好地利用空间,并注意设置适当的边距。