是否可以告诉 latex,它应该根据最终 pdf 文档中的字母顺序对文档中使用的部分进行排序?即我想要这样的内容
\begin{document}
\section{A}
\section{C}
\section{B}
\end{document}
在 pdf 中我得到的结果如下
A
B
C
这可能吗?如果可以,怎么做?由于有大约 300 个部分,因此不建议将各个部分放入单独的文件中(但如果编写了脚本,则可以)。编辑:我也可以将其重写为
\begin{sortEnvironment}{Title}
Content
\end{sortEnvironment}
如果这样会更容易的话。
答案1
(这个答案比我的上一个答案您必须手动创建外部文件并从 shell 提示符运行外部命令。目前,您可以在 LaTeX 中完成所有操作。)
在这个答案中,我使用了您想要使用的 LaTeX 环境。在这里,各部分被写入单独的文件,列表放在主文件中,使用外部 shell 命令对其进行排序,然后在发出命令时输入\PrintSections
。
这是代码。您需要-shell-escape
在执行 latex 或 pdflatex 时提供选项。
\documentclass{article}
\usepackage{amsmath}
\usepackage{lipsum}
\usepackage{newfile}
\makeatletter
% Section list stream
\newoutputstream{sslist}
\openoutputfile{"\jobname.sslist.tex"}{sslist}
\usepackage{environ}
\newoutputstream{ssout}
\NewEnviron{sortEnvironment}[1]{
% Save section file name in external list for input
\addtostream{sslist}{\noexpand\input {"\jobname.#1"}}
% Save the section in an external file
\openoutputfile{"\jobname.#1.tex"}{ssout}
\addtostream{ssout}{\noexpand\section{#1}}
\toks@=\expandafter{\BODY}
\addtostream{ssout}{\the\toks@}}
[\closeoutputstream{ssout}]
\def\PrintSections{%
% Close list stream
\closeoutputstream{sslist}
% Sort list stream using external shell and save in file
\immediate\write18{sort "\jobname.sslist.tex" > "\jobname.sslist.sorted.tex"}
% Get the sorted sections through the above list
\input {"\jobname.sslist.sorted.tex"}
}
\makeatother
\begin{document}
\begin{sortEnvironment}{Title}
Content
\end{sortEnvironment}
\begin{sortEnvironment}{Ab}
Ab, Ab, Ab, Ab, Ab.
\end{sortEnvironment}
\begin{sortEnvironment}{A}
Ab, Ab, Ab, Ab, Ab.
\end{sortEnvironment}
\begin{sortEnvironment}{Zebra}
Zebras have \textit{strips} all over the body.
\end{sortEnvironment}
\begin{sortEnvironment}{Aa}
About AA.
$x = y^2$
\end{sortEnvironment}
\begin{sortEnvironment}{CAD}
CAD as in ``Computer-Aided Design''
or ``Canada Dollar''?
\lipsum[1]
\end{sortEnvironment}
\begin{sortEnvironment}{Name Contains Mathematics $x + y$}
Section name has mathematics.
\end{sortEnvironment}
\begin{sortEnvironment}{Name Does Not Contain Mathematics Ex Plus Wye}
Section name does not have mathematics.
\end{sortEnvironment}
\begin{sortEnvironment}{Section Names Can Have Multiple Words}
\lipsum[2]
\end{sortEnvironment}
\section{Before Sorted Sections}
We have some texts here. The sorted sections are printed after this.
\PrintSections
\section{After Sorted Sections}
And some texts at the end.
\end{document}
这是输出。
答案2
理念
该解决方案使用了一种您不喜欢的技术。即,将部分放在单独的文件中。但是,这是我能找到的唯一(不麻烦的)对部分进行排序的方法。
无论如何,这是你的框架。
(该解决方案基于 Linux(bash)技术,但也应适用于其他操作系统。)
- 将每个部分放在单独的文件中。文件名应与部分名称相同。因此,将 a
\section{Zebra}
放在名为的文件中Zebra.tex
(或者,如果满足排序顺序,您可以使用较短的名称来命名它们)。 - 所有部分文件都保存在单独的目录中,以免打扰主要文件。这里我们使用一个名为的目录
sectionfiles
。 运行脚本
ss.bash
(如下所示)。请注意,第一个处理器之后的其他处理器ls
确保不干预文件扩展名。我们还确保字典顺序。一个简单的ls -1
将生成与此不同的列表。在主文件上运行 LaTeX。
处理器ss.bash
分类器和sectionlist.tex
生成器。
#!/bin/bash
echo "" > sectionlist.tex
for sfile in `ls -1 sectionfiles/*.tex | cut -f 1 -d . | sort -d`
do
echo "\input {$sfile.tex}" >> sectionlist.tex
done
echo "\endinput" >> sectionlist.tex
ss.bash
运行 之后,我的sectionlist.tex
看起来是这样的。
\input {sectionfiles/A.tex}
\input {sectionfiles/Aa.tex}
\input {sectionfiles/Ab.tex}
\input {sectionfiles/B.tex}
\input {sectionfiles/C.tex}
\input {sectionfiles/CAD.tex}
\input {sectionfiles/Cc.tex}
\input {sectionfiles/Dummy.tex}
\input {sectionfiles/Zebra.tex}
\endinput
(简单的)LaTeX 主文件
\documentclass{article}
\usepackage{lipsum}
\begin{document}
\input {sectionlist.tex}
\end{document}
输出页面
最后评论
我理解纯的LaTeX 解决方案本应让我们所有人都更
开心。但这是我能做到的最好了。
答案3
如果你愿意使用环境方法,那么你可以使用datatool
对内容进行排序:
\documentclass{article}
\usepackage{datatool,environ,newfile}
\NewEnviron{sortEnvironment}[1]{{%
\let\par\DTLpar% Cannot include \par in content, so replace \par with \DTLpar
\addtostream{sortOutput}{"#1","\BODY"}% Write section content to output file
}}
\AtBeginDocument{%
\newoutputstream{sortOutput}% New output file
\openoutputfile{sortContent.csv}{sortOutput}% Open output file
}
\AtEndDocument{%
\closeoutputstream{sortOutput}% Close output file
\DTLloaddb[
noheader,
keys={Title,Content}]
{sortedSections}{sortContent.csv}% Load stored content
\dtlsort{Title}{sortedSections}{\dtlcompare}% Sort stored content
\DTLforeach{sortedSections}{\Title=Title,\Content=Content}
{\section{\Title} \Content}% Print all content
}
\begin{document}
\begin{sortEnvironment}{TitleA}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi id diam ante. Quisque
commodo magna lorem, sed vulputate enim fringilla non. In vitae venenatis arcu, nec
pulvinar leo. Aliquam eget magna euismod, sodales mauris sit amet, sagittis lectus.
Mauris sodales nisl turpis, et viverra felis auctor sed. Aliquam ac est lobortis,
condimentum ligula euismod, mattis ligula. Sed sed volutpat eros. Vestibulum quis
enim in nisl consectetur vulputate. Phasellus viverra ac neque et convallis. Pellentesque
blandit porta commodo. Sed semper quis justo in luctus. Sed vel vulputate ligula.
Sed gravida magna dignissim urna eleifend rutrum. Aenean bibendum, risus in tincidunt
consectetur, elit quam luctus lacus, id venenatis nibh erat at tellus. Pellentesque
lacinia dictum nibh a iaculis.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi id diam ante. Quisque
commodo magna lorem, sed vulputate enim fringilla non. In vitae venenatis arcu, nec
pulvinar leo. Aliquam eget magna euismod, sodales mauris sit amet, sagittis lectus.
Mauris sodales nisl turpis, et viverra felis auctor sed. Aliquam ac est lobortis,
condimentum ligula euismod, mattis ligula. Sed sed volutpat eros. Vestibulum quis
enim in nisl consectetur vulputate. Phasellus viverra ac neque et convallis. Pellentesque
blandit porta commodo. Sed semper quis justo in luctus. Sed vel vulputate ligula.
Sed gravida magna dignissim urna eleifend rutrum. Aenean bibendum, risus in tincidunt
consectetur, elit quam luctus lacus, id venenatis nibh erat at tellus. Pellentesque
lacinia dictum nibh a iaculis.
\end{sortEnvironment}
\begin{sortEnvironment}{TitleC}
In hac habitasse platea dictumst. Pellentesque non augue vitae sem tincidunt porttitor
eget in eros. Pellentesque habitant morbi tristique senectus et netus et malesuada
fames ac turpis egestas. Suspendisse quis consectetur tellus. Nam varius velit gravida
ultricies pulvinar. Nunc diam orci, eleifend in turpis at, tristique egestas felis.
Aenean tellus tellus, sagittis sed congue ut, facilisis non lacus. Phasellus volutpat
nec eros vitae congue. Nam maximus dolor eget porta fermentum. Duis ex libero,
fermentum a eleifend sed, consectetur quis erat. Integer sagittis orci et sapien
dignissim, volutpat elementum ligula interdum. Sed aliquam tristique leo et placerat.
\end{sortEnvironment}
\begin{sortEnvironment}{TitleB}
Fusce mattis suscipit leo et eleifend. Pellentesque habitant morbi tristique senectus
et netus et malesuada fames ac turpis egestas. Praesent tincidunt, leo in luctus
elementum, lacus sem suscipit ante, ac rutrum mauris neque eu neque. Pellentesque
ultricies ultricies massa, at aliquam nulla fringilla vitae. Suspendisse at diam
ullamcorper, congue felis a, iaculis eros. Praesent sed diam mauris. Pellentesque
porttitor vitae leo vel dictum. Maecenas vulputate neque vitae massa pulvinar,
viverra facilisis nunc tincidunt. Proin eu eros volutpat turpis egestas malesuada
non a ante. Etiam bibendum turpis sit amet enim commodo sodales. Duis ex metus,
cursus in tellus ut, placerat pharetra enim. Praesent tristique, sapien et aliquam
euismod, nibh massa accumsan eros, sed rhoncus lectus urna eu eros. Aliquam congue
sem id laoreet eleifend. Quisque quis egestas tellus. Aliquam dui dolor, mollis non
neque sit amet, eleifend consectetur lectus.
\end{sortEnvironment}
\end{document}
上面的例子将每个内容写入sortEnvironment
一个 CSV 文件,然后\AtEndDocument
使用datatool
数据库排序功能读取、排序和打印该文件。