这有点复杂,所以让我解释一下我所想知道的它是否是真实存在的。
我有四个.tex
文件名为主文本,tex1.tex,tex2.tex和tex3.tex。
现在,我将把我的实际文件的简化版本放在这里。
第一:
%this is 'tex1'.
\mainenvironment{\mycommand{tag1}{tag2}
Q1 This is a text environment. Here, I put some texts, images, other environments, etc.
}
\mainenvironment{\mycommand{tag2}{tag3}
Q2 This is a text environment. Here, I put some texts, images, other environments, etc.
}
\mainenvironment{\mycommand{tag2}{tag3}
Q3 This is a text environment. Here, I put some texts, images, other environments, etc.
}
下一个:
%this is 'tex2'.
\mainenvironment{\mycommand{tag2}{tag3}
Q4 This is a text environment. Here, I put some texts, images, other environments, etc.
}
\mainenvironment{\mycommand{tag1}{tag3}
Q5 This is a text environment. Here, I put some texts, images, other environments, etc.
}
\mainenvironment{\mycommand{tag2}{tag3}
Q6 This is a text environment. Here, I put some texts, images, other environments, etc.
}
最后一条:
%this is 'tex3'.
\mainenvironment{\mycommand{tag1}{tag2}
Q7 This is a text environment. Here, I put some texts, images, other environments, etc.
}
\mainenvironment{\mycommand{tag1}{tag3}
Q8 This is a text environment. Here, I put some texts, images, other environments, etc.
}
\mainenvironment{\mycommand{tag1}{tag2}
Q9 This is a text environment. Here, I put some texts, images, other environments, etc.
}
现在,对于我来说main.tex
,情况是这样的:
\documentclass{article}
\newcommand{\mainenvironment}[1]{#1}
\usepackage{import}
\begin{document}
\import{tex/}{tex1.tex}
\end{document}
如您所见,我有一个名为 的空命令\mainenvironment
,我希望它在那里以便以后灵活使用(如果我想更改每个文本块的前后内容)。但这不是问题所在。我想知道这是否可行。
我想知道是否可以加载仅有的一些标签我给了那个\mycommand
命令。例如,在\begin{document}
,我会调用一些命令来只加载标签1从我创建的这三个文件中。然后,它只会编译带有 Q1、Q5、Q7、Q8 和 Q9 的文本块。
我认为也许可以改变\mainenvironment
命令使它变得“可标记”,然后使用某些东西来\import
仅加载标记的文本块(即\mainenvironment
命令内的文本),但我无法解决这个特定的难题。
有人可以帮忙吗?
答案1
以下设置可满足您的需要:
\begin{filecontents*}[overwrite]{tex1.tex}
%this is 'tex1'.
\mainenvironment{\mycommand{tag1}{tag2}
Q1 This is a text environment. Here, I put some texts, images, other environments, etc.
}
\mainenvironment{\mycommand{tag2}{tag3}
Q2 This is a text environment. Here, I put some texts, images, other environments, etc.
}
\mainenvironment{\mycommand{tag2}{tag3}
Q3 This is a text environment. Here, I put some texts, images, other environments, etc.
}
\end{filecontents*}
\begin{filecontents*}[overwrite]{tex2.tex}
%this is 'tex2'.
\mainenvironment{\mycommand{tag2}{tag3}
Q4 This is a text environment. Here, I put some texts, images, other environments, etc.
}
\mainenvironment{\mycommand{tag1}{tag3}
Q5 This is a text environment. Here, I put some texts, images, other environments, etc.
}
\mainenvironment{\mycommand{tag2}{tag3}
Q6 This is a text environment. Here, I put some texts, images, other environments, etc.
}
\end{filecontents*}
\begin{filecontents*}[overwrite]{tex3.tex}
%this is 'tex3'.
\mainenvironment{\mycommand{tag1}{tag2}
Q7 This is a text environment. Here, I put some texts, images, other environments, etc.
}
\mainenvironment{\mycommand{tag1}{tag3}
Q8 This is a text environment. Here, I put some texts, images, other environments, etc.
}
\mainenvironment{\mycommand{tag1}{tag2}
Q9 This is a text environment. Here, I put some texts, images, other environments, etc.
}
\end{filecontents*}
\documentclass{article}
\makeatletter
\newcommand{\approvedtag}[1]{\def\@approvedtag{#1}}% Store approved tag
\newcommand{\mycommand}[2]{%
% Assumed \mycommand{<tagA>}{<tagB>} is at the beginning of \mainenvironment
\approvedfalse% Default is to not print anything
\ifnum\pdfstrcmp{#1}{\@approvedtag}=0
\approvedtrue% Approved tag in argument #1
\else
\ifnum\pdfstrcmp{#2}{\@approvedtag}=0
\approvedtrue% Approved tag in argument #2
\fi
\fi
\ignorespaces
\ifapproved% Conditionally print remainder of \mainenvironment
}
\newif\ifapproved
\newcommand{\mainenvironment}[1]{%
#1% Process main environment
\fi% Close condition
}
\makeatother
\begin{document}
\approvedtag{tag1}
\input{tex1}
\input{tex2}
\input{tex3}
\end{document}
\approvedtag{<tag>}
允许您指定要被视为已批准的标签。解决方案会更新\mycommand{<tagA>}{<tagB>}
以测试已批准是否<tag>
匹配<tagA>
或,<tagB>
然后打印其余部分\mainenvironment
。本质上,这假设前面的内容\mycommand
将总是被打印,因此应该是内的第一个元素\mainenvironment
。