
我有一本講義集,其中散佈著練習。我想提供這些練習的關鍵,它是與已編譯的筆記分開的文件,但其相應的 TeX 程式碼與筆記位於同一程式碼中,並引用練習所在的章節/部分。
例如,如果講座的程式碼如下所示:
\begin{document}
\chapter{Lecture 1}
...
\begin{exercise}
What is 2 + 2?
\end{exercise}
\begin{solution}
4
\end{solution}
...
\begin{exercise}
What is 2 * 3?
\end{exercise}
\begin{solution}
6
\end{solution}
\chapter{Lecture 2}
...
\begin{exercise}
How many Canadian provinces are there?
\end{exercise}
\begin{solution}
10
\end{solution}
...
\begin{exercise}
What is the capital of Nova Scotia?
\end{exercise}
\begin{solution}
Halifax
\end{solution}
\end{document}
然後,相應的講義PDF 將包含講座1 和2,其中包含練習,沒有解決方案,但單獨的PDF 包含講座1 中練習的解決方案,另一個PDF 包含講座2 中的解決方案,這些PDF 將知道顯示的內容解決方案來自第一講的練習 2,依此類推。
我能想到的最明顯的解決方案是使用etoolbox
的切換來僅顯示帶有練習的筆記或僅顯示解決方案,但是這樣我就到處都有\iftoggle
並且必須手動創建每個講座的解決方案的PDF,這很快就會變得乏味,如果有幾個講座,或者我是否需要返回並進行更改以更改問題或講座的編號。
(如果在 LaTeX 中沒有好的方法,那麼涉及 shell 腳本的非 TeX 解決方案對我來說沒問題。)
答案1
這是一種使用方法答案包裝和\includeonly
主(驅動程式)文件myfile.tex
\documentclass{book}
\usepackage{answers}
\newtheorem{exercise}{Exercise}[chapter]
\Newassociation{solution}{Soln}{mycor}
\renewcommand{\Solnlabel}[1]{\textbf{Answer #1}}
\includeonly{Lectures}
%\includeonly{Lecture1}
%\includeonly{Lecture2}
\begin{document}
\include{Lectures}
\include{Lecture1}
\include{Lecture2}
\end{document}
主(來源)文件Lectures.tex
\Opensolutionfile{mycor}[Lecture1]
\chapter{Lecture 1}
...
\begin{exercise}
What is 2 + 2?
\begin{solution}
4
\end{solution}
\end{exercise}
...
\begin{exercise}
What is 2 * 3?
\begin{solution}
6
\end{solution}
\end{exercise}
\Closesolutionfile{mycor}
\Opensolutionfile{mycor}[Lecture2]
\chapter{Lecture 2}
...
\begin{exercise}
How many Canadian provinces are there?
\begin{solution}
10
\end{solution}
\end{exercise}
...
\begin{exercise}
What is the capital of Nova Scotia?
\begin{solution}
Halifax
\end{solution}
\end{exercise}
\Closesolutionfile{mycor}