我的源文本很长。我想创建两个输出:
全部文本
仅此源文本的一小部分,通过条件命令选择
MWE(准确地说:不工作)
\documentclass{article}
\usepackage{lipsum}
\begin{document}
\newif\ifPIECES
\PIECEStrue
\section{Lorem ipsum dolor}
\lipsum[1]
\ifPIECES This is first small piece. I want this piece in two outputs.
When PIECES is folse, I want all source text. \else\fi
\lipsum[2]
\ifPIECES This is second small piece. \else\fi
\lipsum[3]
\ifPIECES This is third small piece. \else\fi
\lipsum[4]
\end{document}
我应该如何定义 LaTeX 命令?如你所见,我现在正在编程。
答案1
只需将所有else
语句收集在一起即可减少输入和错误,如下所示。然后您只需将一行代码更改为 false,即PIECESfalse
。
\documentclass{article}
\usepackage{lipsum}
\begin{document}
\newif\ifPIECES
\PIECEStrue
\section{Lorem ipsum dolor}
\lipsum[1]
\ifPIECES This is small piece. I want this piece in my PDF, when PIECES is true.
When PIECES is folse, I want all source text.
This is second small piece. I want second small piece in my PDF, when PIECES is true. When PIECES is folse, I want all source text.
This is third small piece. I want third small piece in my PDF, when PIECES is true.
When PIECES is folse, I want all source text.
\else
\lipsum[2]
\lipsum[3]
\lipsum[4]
\fi
\end{document}
第二个 MWE 按照评论使用命令宏。
\documentclass{article}
\usepackage{lipsum}
\begin{document}
\newif\ifPIECES
\PIECESfalse
\section{Lorem ipsum dolor}
\newcommand\twopieces[2]{%
\ifPIECES
#1
\else
#1#2
\fi
}
\twopieces{This is small piece. I want this piece in my PDF, when PIECES is true.
When PIECES is false, I want all source text.}{ \lipsum[2]}
\end{document}