我正在嘗試使用 xsim 包編寫文檔。它分為幾章,每章末尾都包含自己的練習和解決方案。
目前,每一章的結構都是手動編寫的,如 中所示,ChManual.tex
但我想使用類似的東西macroChapter.tex
,它用作\thechapter
參數來呼叫和列印為 xsim 定義的集合。
如果您使用 ,則以下 MWE 有效ChManual.tex
,如果您取消註解第 1 章和第 2 章的行%\input{macroChapter.tex}
,則它不會顯示錯誤:unknown-collection "collCh\thechapter"
。
\documentclass{report}
\usepackage{filecontents}
\usepackage{xsim}
\DeclareExerciseCollection{collCh1}
\DeclareExerciseCollection{collCh2}
\DeclareExerciseCollection{collChManual}
\begin{filecontents}{Ch1.tex}
\begin{exercise}
This is exercise 1 from chapter \thechapter.
\end{exercise}
\begin{solution}
This is solution of exercise 1 from chapter \thechapter.
\end{solution}
\end{filecontents}
\begin{filecontents}{Ch2.tex}
\begin{exercise}
This is exercise 1 from chapter \thechapter.
\end{exercise}
\begin{solution}
This is solution of exercise 1 from chapter \thechapter.
\end{solution}
\begin{exercise}
This is exercise 2 from chapter \thechapter.
\end{exercise}
\begin{solution}
This is solution of exercise 2 from chapter \thechapter.
\end{solution}
\end{filecontents}
\begin{filecontents}{ChManual.tex}
\begin{exercise}
This is exercise 1 from chapter \thechapter (Manual).
\end{exercise}
\begin{solution}
This is solution of exercise 1 from chapter \thechapter (Manual).
\end{solution}
\begin{exercise}
This is exercise 2 from chapter \thechapter (Manual).
\end{exercise}
\begin{solution}
This is solution of exercise 2 from chapter \thechapter (Manual).
\end{solution}
\end{filecontents}
\begin{filecontents}{macroChapter.tex}
This is chapter \thechapter.
\collectexercises{collCh\thechapter}
\input{Ch\thechapter.tex}
\collectexercisesstop{collCh\thechapter}
\printcollection[print=exercises]{collCh\thechapter}
Bla bla bla bla bla bla bla bla bla bla bla.
\printsolutions[chapter=\thechapter,collection=collCh\thechapter]
\end{filecontents}
\begin{filecontents}{manualChapter.tex}
This is chapter Manual.
\collectexercises{collChManual}
\input{ChManual.tex}
\collectexercisesstop{collChManual}
\printcollection[print=exercises]{collChManual}
Bla bla bla bla bla bla bla bla bla bla bla.
\printsolutions[chapter=3,collection=collChManual]
\end{filecontents}
\begin{document}
\chapter{One}
%\input{macroChapter.tex}
\chapter{Two}
%\input{macroChapter.tex}
\chapter{Manual}
\input{manualChapter.tex}
\end{document}
答案1
這是「在將參數傳遞給下一個宏之前擴展參數」的另一種變體。
具體來說,您需要x
巨集的變體\xsim_start_collection:n
以及適應的使用者命令:\xsim_stop_collection:n
\xsim_print_collection:nn
\ExplSyntaxOn
\RenewDocumentCommand \collectexercises {t!m}
{
\IfBooleanTF {#1}
{ \xsim_start_collection:x {#2} }
{ \xsim_start_collection:n {#2} }
}
\RenewDocumentCommand \collectexercisesstop {t!m}
{
\IfBooleanTF {#1}
{ \xsim_stop_collection:x {#2} }
{ \xsim_stop_collection:n {#2} }
}
\RenewDocumentCommand \printcollection {t!O{}m}
{
\IfBooleanTF {#1}
{ \xsim_print_collection:nx {#2} {#3} }
{ \xsim_print_collection:nn {#2} {#3} }
}
\cs_generate_variant:Nn \xsim_start_collection:n {x}
\cs_generate_variant:Nn \xsim_stop_collection:n {x}
\cs_generate_variant:Nn \xsim_print_collection:nn {nx}
\ExplSyntaxOff
然後使用(注意感嘆號):
\begin{filecontents}{macroChapter.tex}
This is chapter \thechapter.
\collectexercises!{collCh\thechapter}
\input{Ch\thechapter.tex}
\collectexercisesstop!{collCh\thechapter}
\printcollection![print=exercises]{collCh\thechapter}
Bla bla bla bla bla bla bla bla bla bla bla.
\printsolutions[chapter=\thechapter,collection=collCh\thechapter]
\end{filecontents}
順便說一句:我寧願使用集合名稱\arabic{chapter}
而不是\thechapter
- 你需要數字而不是打印輸出!