我学会了如何在论文中创建代码列表在 LaTeX 中设置列表条目。软件包列表我把这个命令放在另一个文件中,其中包含英文字块\lstlistingname{Code}
和\lstlistlistingname{List of Codes}
。
但是我的论文是用多种语言写的,因此稍后我需要重新创建代码表标题\lstlistingname{Códigos}
和\lstlistlistingname{Lista de Códigos}
当我切换论文语言时,正如问题中解释的那样我可以将我的翻译与原文保留在一起吗?
% Calculate the size of the header
\newcommand{\calculatelisteningsheader}
{
\renewcommand\cftlstlistingpresnum{\lstlistingname~}
\settowidth\mylen{\cftlstlistingpresnum\cftlstlistingaftersnum}
\addtolength\cftlstlistingnumwidth{\mylen} %
\renewcommand\cftlstlistingaftersnum{\hfill\textendash\hfill}
}
在下面的例子中,我创建了命令\calculatelisteningsheader
来重新评估标题的大小:
\documentclass[12pt,openright,oneside,a4paper]{abntex2}
\usepackage{listings}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
% Setting Entries of List of Listings in LaTeX. Package Listings
% http://tex.stackexchange.com/questions/228936/setting-entries-of-list-of-listings
\newlength\mylen
\renewcommand\lstlistingname{Code}
\renewcommand\lstlistlistingname{List of Codes}
\begingroup
\makeatletter
\let\newcounter\@gobble\let\setcounter\@gobbletwo
\globaldefs\@ne \let\c@loldepth\@ne
\newlistof{listings}{lol}{\lstlistlistingname}
\newlistof{lstlistoflistings}{lol}{\lstlistlistingname}
\newlistentry{lstlisting}{lol}{0}
\makeatother
\endgroup
% Calculate the size of the header
\newcommand{\calculatelisteningsheader}
{
\renewcommand\cftlstlistingpresnum{\lstlistingname~}
\settowidth\mylen{\cftlstlistingpresnum\cftlstlistingaftersnum}
\addtolength\cftlstlistingnumwidth{\mylen} %
\renewcommand\cftlstlistingaftersnum{\hfill\textendash\hfill}
}
% Ensure it is called at least one time
\calculatelisteningsheader
% Later...
\renewcommand\lstlistingname{Code}
\renewcommand\lstlistlistingname{List of Codes}
\calculatelisteningsheader
\begin{document}
\lstlistoflistings
\begin{lstlisting}[caption={First Code}]
code1
\end{lstlisting}
\begin{lstlisting}[caption={Second Code}]
code2
\end{lstlisting}
\end{document}
然而,有些事情出了问题。第二次调用时,空白区域的大小增加了一倍多\calculatelisteningsheader
:
为什么每次调用时空间大小都会增加\calculatelisteningsheader
?
更新
根据建议@koleygrso,我已经尝试删除缩进并添加,%
但\calculatelisteningsheader
结果仍然相同:
\newcommand{\calculatelisteningsheader}
{%
\renewcommand\cftlstlistingpresnum{\lstlistingname~}
\settowidth\mylen{\cftlstlistingpresnum\cftlstlistingaftersnum}
\addtolength\cftlstlistingnumwidth{\mylen} %
\renewcommand\cftlstlistingaftersnum{\hfill\textendash\hfill}
}
答案1
问题是每次\calculatelisteningsheader
调用时,\cftlstlistingnumwidth
长度都会增加(因为\addtolength
)。
一种快速的解决方法是\cftlstlistingnumwidth
预先将旧的存储为长度,在\calculatelisteningsheader
宏之外,并在“添加到长度”时使用它\cftlstlistingnumwidth
。 这样,即使\calculatelisteningsheader
多次调用,总和仍然保持一致。
请参阅下面的代码——我已标记出相关的更改。
\documentclass[12pt,openright,oneside,a4paper]{abntex2}
\usepackage{listings}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
% Setting Entries of List of Listings in LaTeX. Package Listings
% http://tex.stackexchange.com/questions/228936/setting-entries-of-list-of-listings
\newlength\mylen
\renewcommand\lstlistingname{Code}
\renewcommand\lstlistlistingname{List of Codes}
\begingroup
\makeatletter
\let\newcounter\@gobble\let\setcounter\@gobbletwo
\globaldefs\@ne \let\c@loldepth\@ne
\newlistof{listings}{lol}{\lstlistlistingname}
\newlistof{lstlistoflistings}{lol}{\lstlistlistingname}
\newlistentry{lstlisting}{lol}{0}
\makeatother
\endgroup
\newlength\cftlstlistingoldnumwidth% <---------
\setlength\cftlstlistingoldnumwidth{\cftlstlistingnumwidth}% <---------
% Calculate the size of the header
\newcommand{\calculatelisteningsheader}
{%
\renewcommand\cftlstlistingpresnum{\lstlistingname~}%
\settowidth\mylen{\cftlstlistingpresnum\cftlstlistingaftersnum}%
\setlength\cftlstlistingnumwidth{\dimexpr\cftlstlistingoldnumwidth+\mylen}% <---------
\renewcommand\cftlstlistingaftersnum{\hfill\textendash\hfill}%
}
% Ensure it is called at least one time
\calculatelisteningsheader
% Later...
\renewcommand\lstlistingname{Code}
\renewcommand\lstlistlistingname{List of Codes}
\calculatelisteningsheader
\calculatelisteningsheader % <------- For testing purposes
\calculatelisteningsheader % <------- For testing purposes
\calculatelisteningsheader % <------- For testing purposes
\begin{document}
\lstlistoflistings
\begin{lstlisting}[caption={First Code}]
code1
\end{lstlisting}
\begin{lstlisting}[caption={Second Code}]
code2
\end{lstlisting}
\end{document}