我想自动生成一个以逗号分隔的列表,列出我的 .tex 文件中出现的特定命令的所有列表。
具体来说,我想确保使用该chemstyle
包并\compound{<structurelabel>}
在长文档中正确编号化学结构,方法是首先以不可见的方式声明所有结构,按照它们在主内容中出现的顺序\compound*{<1ststructurelabel>,<2ndstructurelabel>,...}
。[注意:由于在标题和目录中包含了对结构的引用,因此可能会出现编号不正确的情况]。
我知道这个出色的在线工具,但发现它有时运行完美,有时却根本没有反应(据我所知,我每次都尝试以完全相同的方式使用它),所以很希望能有替代方案。
以下是一个 MWE,希望能使我的查询更清晰(我使用 Texmaker 和 MiKTeX,安装了 ActivePerl 并且启用了 shell-escape):
\documentclass[oneside,fontsize=13pt,parskip=full]{scrbook}
\usepackage{scrhack,graphicx,libertine,lipsum,caption,setspace,gensymb,amsmath,amsthm,array,lastpage,color,upgreek,soul,multirow}
\usepackage[left=1.75in,right=1in,top=1.3in,bottom=1.3in]{geometry}
\usepackage[runs=2]{auto-pst-pdf}
\usepackage{chemstyle}
\usepackage[autostyle]{csquotes}
\usepackage[english]{babel}
\usepackage[
backend=biber,
style=numeric,
autocite=superscript,
url=false,
doi=false,
natbib=true
]{biblatex}
\defbibheading{bibintoc}[\bibname]{%
\addchap{#1}%
}
\usepackage[flushleft]{threeparttable}
\RawFloats[figure,scheme]
\newcommand\fnote[1]{\captionsetup{font=small,skip=4pt}\caption*{#1}}
\newcommand*{\myfont}{\fontfamily{lmr}\selectfont}
\newcommand{\mytilde}{\raise.17ex\hbox{$\scriptstyle\sim$}}
\definecolor{ilinks}{rgb}{0.356,0.064,0.796}
\renewcommand*{\schemerefformat}{%
\fontsize{12pt}{12pt}\color{magenta}\textit%
}
\captionsetup[figure]{labelfont={color=ilinks}}
\captionsetup[scheme]{labelfont={color=ilinks}}
\captionsetup[table]{labelfont={color=ilinks}}
\addtokomafont{part}{\fontsize{43}{1.5em}\mdseries\scshape}
\addtokomafont{chapter}{\Huge\scshape}
\addtokomafont{section}{\mdseries\fontsize{19}{22}\normalfont\itshape}
\addtokomafont{subsection}{\fontsize{15}{18}\selectfont\bfseries}
\addtokomafont{subsubsection}{\fontsize{14}{17}\selectfont\mdseries\scshape}
\usepackage{fancyhdr}
\fancypagestyle{main}{ %
\fancyhf{}
\fancyhead[L]{\rightmark}
\fancyhead[R]{\leftmark}
\fancyfoot[C]{}
\fancyfoot[R]{\thepage}
\renewcommand{\headrulewidth}{0.4pt}}
\newcommand{\markedsection}[2]{\section[#2]{#2%
\sectionmark{#1}}
\sectionmark{#1}}
\newcommand{\markedchapter}[2]{\chapter[#2]{#2%
\chaptermark{#1}}
\chaptermark{#1}}
\fancypagestyle{plain}{%
\fancyhf{}
\fancyfoot[R]{\thepage}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}}
\usepackage[nottoc]{tocbibind}
\setcounter{tocdepth}{3}
\newcommand*{\TitleFont}{\mdseries\scshape
\fontsize{43}{1.5em}
\selectfont}
\newcommand*{\AuthorFont}{\sffamily%
\fontsize{25}{15}%
\selectfont}
\newcommand*{\DateFont}{%
\fontsize{19}{1.2em}%
\selectfont}
\newcommand*{\PublishersFont}{%
\fontsize{19}{1.2em}%
\selectfont}
\usepackage[colorlinks,linkcolor=black,citecolor=black]{hyperref}
\renewcommand*{\sectionautorefname}{Section}
\renewcommand*{\subsectionautorefname}{Section}
\renewcommand*{\subsubsectionautorefname}{Section}
\newcolumntype{P}%
{>{\centering\arraybackslash}%
m{2cm}%
<{}}
\newcolumntype{C}[1]%
{>{\centering\arraybackslash}%
p{#1}}
\newcolumntype{N}%
{@{}m{0pt}%
@{}}
\usepackage{microtype}
\begin{document}
\frontmatter
\tableofcontents
\mainmatter
\section{Reference to \compound{one}}
Jumbled references to \compound{one}, then \compound{two}, \compound{three} and \compound{four}. Now repeats of \compound{three} and \compound{one}.
\section{Reference to \compound{four}}
\end{document}
答案1
一种方法是修补\compound
:etoolbox
软件包包含允许将代码添加到已定义的命令前面或后面的命令。请参阅这个帖子以便对此做出很好的解释。
为了获得以逗号分隔的列表,我们将修补命令,以便在调用它时将其参数写入文件:
\usepackage{etoolbox}
\newwrite\myfile
\immediate\openout\myfile=\jobname.foo
\apptocmd{\compound}{\write\myfile{#1,}}{}{}
这是一个完整的小例子。在这个代码中,\term
标记了定义,但也自动处理了索引。
\documentclass{article}
\usepackage{makeidx}
\usepackage{etoolbox}
\newwrite\myfile
\immediate\openout\myfile=\jobname.foo
\newcommand{\term}[1]{\emph{#1}\index{#1}}
\apptocmd{\term}{\write\myfile{#1,}}{}{}
\makeindex
\begin{document}
The \term{mitochondria} are the powerhouse of the cell. \term{Anaphora}
is the repetition of a word at the start of several phrases. A
\term{circle} is a set of points in the plane equidistant from a
given point, called its \term{center}.\footnote{The distance
itself is called the \term{radius}.}
\printindex
\end{document}
如果此文件名为defn.tex
,则编译后,defn.foo
包含我提供的所有参数的逗号分隔列表\term
:
mitochondria,
Anaphora,
circle,
center,
radius,