使用条件和/或注释管理同一文档中的两个版本

使用条件和/或注释管理同一文档中的两个版本

我有一个文档,需要管理多个版本,用于多种语言。到目前为止,我为每种语言保留了一个不同的 LaTeX 项目,但每次都必须打开、编辑和保存多个项目,这有点烦人。我希望能够“标记”文档的各个部分,并且根据选择的“版本”选项,只在 PDF 文件中显示相应的标记部分。

文本散布在文档中,因此保留不同的 tex 文件并有条件地包含它们是行不通的。也不想更改文档类别。

我发现最好的选择是声明一个环境,用注释声明包围文本块,如下所示:

\usepackage{verbatim}
\newenvironment{v1}{}{} % to show
% \newenvironment{v1}{\comment}{\endcomment} % to hide

(...)

\begin{v1}
   (...)
\end{v1}

好吧,\comment直接\endcomment使用不起作用,即使直接包含在文本中,我也会收到来自 Overleaf 的致命错误。我还尝试导入comment而不是verbatim,结果相同。

\begin{comment}并且\end{comment}通常(并非总是)在直接包含在文本中时起作用,但在环境声明中使用时也会引发致命错误(如\newenvironment{v1}{\begin{comment}}{\end{comment}})。

我遇到的错误取决于我试图注释掉的具体内容。我总是收到LaTeX3 Error: Command '\FA' already defined!来自某个模板文件的错误(该文件未被编辑),以及有关失控参数或缺失的投诉}(我已检查过,似乎并不存在)。

在环境声明中使用\begin{comment}和的正确方法是什么?还有其他方法可以达到类似的效果吗?\end{comment}

答案1

这里提供一种使用包来根据需要组合各个部分的方法codesection

基本思想是:

  • 根据需要定义标志
  • 使用对标志敏感的环境。

标志:我在这里为英语或德语文本部分定义了EG。您可以自由使用任意数量的此类标志,并使用您喜欢的名称。要切换版本,请设置这些标志,然后进行编译。使用两个标志,您将有 4 种情况,如注释行中所述。或者使用\SetCodeSection,请参阅手册。

% ~~~ define as you need it, e.g. Engl+Germn ~~~
%     tf->E, ft->G, tt->both, ff->none
\DefineCodeSection[true]{E}
\DefineCodeSection[true]{G}

部件/版本:您使用该软件包提供的特殊环境。您需要做的就是输入您的标志名称。里面使用常规 LaTeX 命令。

  % ~~~ for the part in English ~~~~
  \BeginCodeSection{E}

  \EndCodeSection{E}

为了简单起见,我将两个标志都设置为 true 作为示例。您会看到:

  • 两种语言按代码给出的顺序出现
  • 部分编号照常递增,因为它现在编译了两个部分
  • 如果将标志设置为互斥且两种语言的所有内容都并行,则每种语言的章节编号都会很好。

结果结果

\documentclass[10pt]{article}
\usepackage[english,ngerman]{babel}% language support
\usepackage{codesection}

% ~~~ define as you need it, e.g. Engl+Germn ~~~
%     tf->E, ft->G, tt->both, ff->none
\DefineCodeSection[true]{E}
\DefineCodeSection[true]{G}

\begin{document}
  % ~~~ some text in English ~~~~~~~
  \BeginCodeSection{E}
    \section{Down the Rabbit-Hole}
    
    Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do: once or twice she had peeped into the book her sister was reading, but it had no pictures or conversations in it, “and what is the use of a book,” thought Alice “without pictures or conversations?” 
  \EndCodeSection{E}

  % ~~~ some text in German ~~
  \BeginCodeSection{G}
    \section{Das weiße Kaninchen}
    
    Ich könnte ja Gänseblümchen pflücken und daraus eine Kette flechten, dachte Alice gerade schläfrig bei sich, als sie plötzlich ein Weißes Kaninchen mit rosarot funkelnden Augen über die Wiese nahe am Fluss kommen sah. 
  \EndCodeSection{G}
\end{document}

附言:虽然此设置

% ~~~ define as you need it, e.g. Engl+Germn 
%     tf->E, ft->G, tt->both, ff->none
\DefineCodeSection[true]{E}
\DefineCodeSection[false]{G}

使用相同的代码创建以下内容:

英语

相关内容