跟进上一个问题关于为我的学生准备的数百个练习的通用处理出现了一个新问题,我试图解决...但事实是我不知道 LaTeX 是否可以处理我尝试做的事情,也不知道该怎么做......
每个练习都是一个单独的小文件,涉及整个物理学领域。我输入了一些旗帜在这些单独的文件中,例如能量、熵、固体等,以便我可以对它们进行排序。
嗯,我的问题。
有没有办法通过这些标志来打印仅引用我在主文档中选择的标志的输入文件,而不打印所有其他文件?也就是说,如果我只想要包含旗熵有没有办法不打印exercise.436
不包含的旗熵...说类似的话\chose{entropy}
\documentclass[12pt]{book}
\usepackage{xcolor}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% This piece of code its from previous question
%% And is due to @egreg
\newif\ifflags
\long\def\flags[#1]{%
\ifflags
\par
\begingroup
\let \\ \par
\color{red}\small\\ \textbf{Flags: #1}%
\par\medskip
\endgroup
\fi}
\flagstrue % comment out for don't appears the flags
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\input{exercise.435.tex}
\input{exercise.436.tex}
\end{document
exercise.435
开头的文件包含以下几行
\flags[energy, entropy, solid.boby]
here is the text of exercise.435.tex
exercise.436
开头的文件包含以下几行
\flags[energy, solid.boby]
here is the text of exercise.436.tex
答案1
这是使用 LaTeX3 宏的方法
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\enableflags}{ m }
{
\karathan_enable_flags:n { #1 }
}
\NewDocumentCommand{\flags}{ O{} }
{
\karathan_flags:n { #1 }
}
\seq_new:N \g_karathan_enabled_flags_seq
\bool_new:N \l_karathan_flag_found_bool
\cs_new_protected:Npn \karathan_enable_flags:n #1
{
\seq_gset_from_clist:Nn \g_karathan_enabled_flags_seq { #1 }
}
\cs_new_protected:Npn \karathan_flags:n #1
{
\bool_set_false:N \l_karathan_flag_found_bool
\clist_map_inline:nn { #1 }
{
\seq_if_in:NnT \g_karathan_enabled_flags_seq { ##1 }
{ \clist_map_break:n { \bool_set_true:N \l_karathan_flag_found_bool } }
}
\bool_if:NF \l_karathan_flag_found_bool { \endinput }
}
\ExplSyntaxOff
\begin{document}
\section{Enabled \texttt{energy}}
\enableflags{energy}
\input{exercise.435.tex}
\input{exercise.436.tex}
\section{Enabled \texttt{entropy}}
\enableflags{entropy}
\input{exercise.435.tex}
\input{exercise.436.tex}
\section{Enabled \texttt{solid.body}}
\enableflags{solid.body}
\input{exercise.435.tex}
\input{exercise.436.tex}
\section{Enabled \texttt{energy}, \texttt{entropy}, \texttt{solid.body}}
\enableflags{energy, entropy, solid.body}
\input{exercise.435.tex}
\input{exercise.436.tex}
\end{document}
该\enableflags
命令接收您要启用的标志列表作为参数。然后\flags
将其参数中的每个项目与此列表进行比较。如果没有找到匹配项,则关闭该文件并且不再接受来自该文件的输入(使用\endinput
)。
如果你想要一个和而不是或者标志,下面是定义宏的方法\karathan_flags:n
:
\cs_new_protected:Npn \karathan_flags:n #1
{
\bool_set_true:N \l_karathan_flag_found_bool
\seq_map_inline:Nn \g_karathan_enabled_flags_seq
{
\clist_if_in:nnF { #1 } { ##1 }
{ \seq_map_break:n { \bool_set_false:N \l_karathan_flag_found_bool } }
}
\bool_if:NF \l_karathan_flag_found_bool { \endinput }
}
如果全部设置标志在的参数中\flags
,那么布尔值保持为真并且\endinput
不会被执行。
这是一个完整的例子:
\documentclass{article}
\usepackage{xparse,indentfirst}
\ExplSyntaxOn
\NewDocumentCommand{\enableflags}{ m }
{
\karathan_enable_flags:n { #1 }
}
\NewDocumentCommand{\resetflags}{ m }
{
\seq_gclear:N \g_karathan_enabled_flags_seq
\karathan_enable_flags:n { #1 }
}
\NewDocumentCommand{\flags}{ O{} }
{
\karathan_flags:n { #1 }
}
\seq_new:N \g_karathan_enabled_flags_seq
\bool_new:N \l_karathan_flag_found_bool
\cs_new_protected:Npn \karathan_enable_flags:n #1
{
\seq_gset_from_clist:Nn \g_karathan_enabled_flags_seq { #1 }
}
\cs_new_protected:Npn \karathan_flags:n #1
{
\bool_set_true:N \l_karathan_flag_found_bool
\seq_map_inline:Nn \g_karathan_enabled_flags_seq
{
\clist_if_in:nnF { #1 } { ##1 }
{ \seq_map_break:n { \bool_set_false:N \l_karathan_flag_found_bool } }
}
\bool_if:NF \l_karathan_flag_found_bool
%{ \texttt{(#1)}\par }
{ \endinput }
}
\ExplSyntaxOff
\begin{document}
\section{Enabled \texttt{energy}}
\enableflags{energy}
\input{exercise.435.tex}
\input{exercise.436.tex}
\section{Enabled \texttt{entropy}}
\enableflags{entropy}
\input{exercise.435.tex}
\input{exercise.436.tex}
\section{Enabled \texttt{energy}, \texttt{entropy}}
\enableflags{energy,entropy}
\input{exercise.435.tex}
\input{exercise.436.tex}
\section{Enabled \texttt{solid.body}}
\enableflags{solid.body}
\input{exercise.435.tex}
\input{exercise.436.tex}
\section{Enabled \texttt{energy}, \texttt{entropy}, \texttt{solid.body}}
\enableflags{energy, entropy, solid.body}
\input{exercise.435.tex}
\input{exercise.436.tex}
\end{document}
答案2
此版本仅使用原始的 TeX 条件,您可以在单独的命令中指定任意数量的标志\chooseflag
,如果文件包含任何选定的标志,则会打印该文件。
\documentclass[12pt]{book}
\usepackage{xcolor}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% This piece of code its from previous question
%% And is due to @egreg
\newif\ifflags
\long\def\flags[#1]{%
{\xflags#1,xstopx,\endinput%
\ifx\endinput\relax
\ifflags
\par
\begingroup
\let \\ \par
\color{red}\small\\ \textbf{Flags: #1}%
\par\medskip
\endgroup
\fi\fi}}
\def\xflags#1,#2{%
\csname xflags#1\endcsname
\xflags#2}
\def\xflagsxstopx#1{}
\def\chooseflag#1{\expandafter\def\csname xflags#1\endcsname{\let\endinput\relax}}
\chooseflag{entropy}
\flagstrue % comment out for don't appears the flags
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\input{exercise.435.tex}
\input{exercise.436.tex}
\end{document}