如果我尝试将列表中的分数积分,列表周围的框架就会变得不连续。有没有办法修复它,而不用将其变成\frac
一行/
?提前致谢。
\documentclass{report}
\usepackage{listings}
\usepackage{xcolor}
\lstset{ %
language=C++, % choose the language of the code
basicstyle=\footnotesize, % the size of the fonts that are used for the code
numbers=left, % where to put the line-numbers
numberstyle=\footnotesize, % the size of the fonts that are used for the line-numbers
commentstyle=\color{DarkGreenComment}\ttfamily,
%keywordstyle=\color{blue}\ttfamily,
stepnumber=1, % the step between two line-numbers. If it is 1 each line will be numbered
numbersep=5pt, % how far the line-numbers are from the code
backgroundcolor=\color{white}, % choose the background color. You must add \usepackage{color}
showspaces=false, % show spaces adding particular underscores
showstringspaces=false, % underline spaces within strings
showtabs=false, % show tabs within strings adding particular underscores
frame=single, % adds a frame around the code
tabsize=2, % sets default tabsize to 2 spaces
captionpos=b, % sets the caption-position to bottom
breaklines=true, % sets automatic line breaking
alsoletter={]},
breakatwhitespace=false, % sets if automatic breaks should only happen at whitespace
emph={createBaseElement, createJunction},
morekeywords={TreeGraph,Graph,IndexGraph,HashGraph,CompressedGraph,EdgeMap,NodeMap,EdgeEntry},
%breakatwhitespace,
escapeinside={\%*}{*)} % if you want to add a comment within your code
}
\begin{document}
\begin{lstlisting}[label={lst:pcgoptimized}, mathescape=true ,caption={Version of PCG as employed by NAG library.}]
$\mathbf{r}_0 = \mathbf{b} - \mathbf{A} * \mathbf{x}_0$
$i = 0$
do {
solve $\mathbf{C} * \mathbf{z}_{i} = \mathbf{r}_{i}$ $\Rightarrow$ $\mathbf{z}_{i} = \mathbf{C}^{-1} * \mathbf{r}_{i}$
$i = i + 1$
if ($i==1$)
$\mathbf{p}_i = \mathbf{z}_{i-1}$
else {
$b_i = \frac{\displaystyle \mathbf{r}_{i-1}^T * \mathbf{z}_{i-1}}{\displaystyle \mathbf{r}_{i-2}^T * \mathbf{z}_{k-2}}$
$\mathbf{p}_k = \mathbf{z}_{i-1} + b_i * \mathbf{p}_{i-1}$
}
$\mathbf{q}_i = \mathbf{A} * \mathbf{p}_k$
$a_i = \frac{\displaystyle \mathbf{r}_{i-1}^T * \mathbf{z}_{i-1}}{\displaystyle \mathbf{p}_k^T * \mathbf{q}_i}$
$\mathbf{x}_i = \mathbf{x}_{i-1} + a_i * \mathbf{p}_i$
$\mathbf{r}_i = \mathbf{r}_{i-1} - a_i * \mathbf{q}_i$
}
while (termination criterion not fulfilled)
\end{lstlisting}
\end{document}
答案1
注意模糊因子(-3.5pt,5pt)
和(3pt,14.5pt)
。我尝试在源代码中挖掘\lst@sep
或之类的东西,\lst@skip
但找不到任何有用的东西。
不要忘记运行两次。
\documentclass{report}
\usepackage{listings}
\usepackage{xcolor}
\usepackage{showframe}% debugging tool
\usepackage{tikz}
\usetikzlibrary{tikzmark,calc}
\usepackage{caption}
\lstset{ %
language=C++, % choose the language of the code
basicstyle=\footnotesize, % the size of the fonts that are used for the code
numbers=left, % where to put the line-numbers
numberstyle=\footnotesize, % the size of the fonts that are used for the line-numbers
commentstyle=\color{DarkGreenComment}\ttfamily,
%keywordstyle=\color{blue}\ttfamily,
stepnumber=1, % the step between two line-numbers. If it is 1 each line will be numbered
numbersep=5pt, % how far the line-numbers are from the code
backgroundcolor=\color{white}, % choose the background color. You must add \usepackage{color}
showspaces=false, % show spaces adding particular underscores
showstringspaces=false, % underline spaces within strings
showtabs=false, % show tabs within strings adding particular underscores
frame=single, % adds a frame around the code
tabsize=2, % sets default tabsize to 2 spaces
captionpos=b, % sets the caption-position to bottom
breaklines=true, % sets automatic line breaking
alsoletter={]},
breakatwhitespace=false, % sets if automatic breaks should only happen at whitespace
emph={createBaseElement, createJunction},
morekeywords={TreeGraph,Graph,IndexGraph,HashGraph,CompressedGraph,EdgeMap,NodeMap,EdgeEntry},
%breakatwhitespace,
escapeinside={\%*}{*)} % if you want to add a comment within your code
}
\begin{document}
\noindent\tikzmark{start}\vspace{-\baselineskip}
\begin{lstlisting}[label={lst:pcgoptimized}, mathescape=true]
$\mathbf{r}_0 = \mathbf{b} - \mathbf{A} * \mathbf{x}_0$
$i = 0$
do {
solve $\mathbf{C} * \mathbf{z}_{i} = \mathbf{r}_{i}$ $\Rightarrow$ $\mathbf{z}_{i} = \mathbf{C}^{-1} * \mathbf{r}_{i}$
$i = i + 1$
if ($i==1$)
$\mathbf{p}_i = \mathbf{z}_{i-1}$
else {
$b_i = \frac{\displaystyle \mathbf{r}_{i-1}^T * \mathbf{z}_{i-1}}{\displaystyle \mathbf{r}_{i-2}^T * \mathbf{z}_{k-2}}$
$\mathbf{p}_k = \mathbf{z}_{i-1} + b_i * \mathbf{p}_{i-1}$
}
$\mathbf{q}_i = \mathbf{A} * \mathbf{p}_k$
$a_i = \frac{\displaystyle \mathbf{r}_{i-1}^T * \mathbf{z}_{i-1}}{\displaystyle \mathbf{p}_k^T * \mathbf{q}_i}$
$\mathbf{x}_i = \mathbf{x}_{i-1} + a_i * \mathbf{p}_i$
$\mathbf{r}_i = \mathbf{r}_{i-1} - a_i * \mathbf{q}_i$
}
while (termination criterion not fulfilled)
\end{lstlisting}% automatic par
\hspace*{\fill}\tikzmark{end}\vspace{-\baselineskip}%
\captionof{lstlisting}{Version of PCG as employed by NAG library.}
\begin{tikzpicture}[remember picture,overlay]
\draw[red] ($(pic cs:start)+(-3.5pt,5pt)$) rectangle ($(pic cs:end)+(3pt,14.5pt)$);
\end{tikzpicture}
\end{document}