1
我不明白为什么下一个 MWE 中的代码不会导致在生成的 pdf 文档中从 开始对 Cpp 语言行进行编号(我在pdfTeX
)。
\documentclass{memoir}
\usepackage{xcolor,minted}
\usemintedstyle{friendly}
\newminted{cpp}{
linenos=true,
fontsize=\footnotesize,
fontseries=m,
frame=leftline,
tabsize=4,
firstnumber=1,
stepnumber=5
}
\newmintedfile{cpp}{
linenos=true,
fontsize=\footnotesize,
fontseries=m,
samepage=false,
tabsize=4,
firstnumber=1,
stepnumber=5
}
\newmint{cpp}{
linenos=true,
fontsize=\footnotesize,
fontseries=m,
frame=leftline,
tabsize=4,
firstnumber=1,
stepnumber=5
}
\renewcommand{\theFancyVerbLine}{\tiny\ttfamily%
\textcolor[rgb]{0,0,0}{\arabic{FancyVerbLine}}%
}
\begin{document}
\cppfile[linenos=true]{Task4.cpp}
\end{document}
文件Task4.cpp中的Cpp语言代码如下:
void xchanger(int na, int nb)
{
int naux;
naux = na, na = nb, nb = naux;
printf("Now, numbers are as follows within the function:\n");
printf("Number 1: %d\n", na);
printf("Number 2: %d\n", nb);
return;
}
有人可以复制这种不当行为吗?
编辑:说实话,Cpp 文件中的代码比这个要大,而且我通过发出以下命令仅排版上面的部分
\cppfile[linenos=true,firstline=14,lastline=25]{Task4.cpp}
但这似乎并没有什么区别。
答案1
你可以通过 patch 来给第一行编号fancyvrb
。在序言中插入以下代码。我标记了我添加的部分;它位于末尾。
\makeatletter
%% DG modification begin - Dec. 20, 1995
%%\def\FV@Numbers@left{%
%% \def\FV@LeftListNumber{\hbox to\z@{%
%% \hss\theFancyVerbLine\kern\FV@NumberSep}}}
\def\FV@Numbers@left{%
%% DG/SR modification begin - Apr. 28, 1998
\let\FV@RightListNumber\relax
%% DG/SR modification end
\def\FV@LeftListNumber{%
\@tempcnta=\FV@CodeLineNo
\@tempcntb=\FV@CodeLineNo
\divide\@tempcntb\FV@StepNumber
\multiply\@tempcntb\FV@StepNumber
\ifnum\@tempcnta=\@tempcntb
%% DG/SR modification begin - Apr. 28, 1998
%% \hbox to\z@{\hss\theFancyVerbLine\kern\FV@NumberSep}%
\if@FV@NumberBlankLines
\hbox to\z@{\hss\theFancyVerbLine\kern\FV@NumberSep}%
\else
\ifx\FV@Line\empty
\else
\hbox to\z@{\hss\theFancyVerbLine\kern\FV@NumberSep}%
\fi
\fi
%% DG/SR modification end
%% Line 1 modification begin
\else
\ifnum\@tempcnta=1
\if@FV@NumberBlankLines
\hbox to\z@{\hss\theFancyVerbLine\kern\FV@NumberSep}%
\else
\ifx\FV@Line\empty
\else
\hbox to\z@{\hss\theFancyVerbLine\kern\FV@NumberSep}%
\fi
\fi
\fi
%% Line 1 modification end
\fi}}
\makeatother
编辑
原始版本适用于外部文件,但仅限于firstline=1
。下面是适用于所有情况的修改版本。作为附加奖励,它始终对 的倍数进行编号stepnumber
,而不是对具有偏移的倍数进行编号。
\makeatletter
\newcount\FancyVerbOffset
\newcount\@tempcntc
%% DG modification begin - Dec. 20, 1995
%%\def\FV@Numbers@left{%
%% \def\FV@LeftListNumber{\hbox to\z@{%
%% \hss\theFancyVerbLine\kern\FV@NumberSep}}}
\def\FV@Numbers@left{%
%% DG/SR modification begin - Apr. 28, 1998
\let\FV@RightListNumber\relax
%% DG/SR modification end
\def\FV@LeftListNumber{%
\@tempcnta=\FV@CodeLineNo
\@tempcntb=\FV@CodeLineNo
%% Line 1 modification begin
\ifnum\FancyVerbStartNum<\tw@\else
\@tempcntc=\FancyVerbStartNum
\divide\@tempcntc\FV@StepNumber
\multiply\@tempcntc\FV@StepNumber
\FancyVerbOffset=\FancyVerbStartNum
\advance\FancyVerbOffset-\@tempcntc
\advance\FancyVerbOffset\m@ne
\advance\@tempcnta-\FancyVerbOffset
\advance\@tempcntb-\FancyVerbOffset
\fi
%% Line 1 modification end
\divide\@tempcntb\FV@StepNumber
\multiply\@tempcntb\FV@StepNumber
\ifnum\@tempcnta=\@tempcntb
%% DG/SR modification begin - Apr. 28, 1998
%% \hbox to\z@{\hss\theFancyVerbLine\kern\FV@NumberSep}%
\if@FV@NumberBlankLines
\hbox to\z@{\hss\theFancyVerbLine\kern\FV@NumberSep}%
\else
\ifx\FV@Line\empty
\else
\hbox to\z@{\hss\theFancyVerbLine\kern\FV@NumberSep}%
\fi
\fi
%% DG/SR modification end
%% Line 1 modification begin
\else
\ifnum\c@FancyVerbLine=1
\if@FV@NumberBlankLines
\hbox to\z@{\hss\theFancyVerbLine\kern\FV@NumberSep}%
\else
\ifx\FV@Line\empty
\else
\hbox to\z@{\hss\theFancyVerbLine\kern\FV@NumberSep}%
\fi
\fi
\fi
%% Line 1 modification end
\fi}}
\makeatother