
我想為我的學生寫一些教程,為此我使用exam
文件類和solution
環境。
現在的問題是我有一個聲明
\printanswers
我需要對教程進行評論,並取消對解決方案的評論。
我希望是否有一種方法可以一次編譯它,並且源文件(例如tutorial.tex)創建兩個pdf -
- 教程.pdf(無解決方案)
- tutorialSolution.pdf(含解決方案)
以下是我的原始碼 -
\documentclass{exam}
%\printanswers
\usepackage[T1]{fontenc}
\usepackage{pslatex}
\usepackage[pdftex]{color}
\usepackage[pdftex]{graphicx}
\begin{document}
\begin{questions}
\vskip 0.5 cm \question Question header \vskip 0.5cm
Question text
\begin{solution}
Solution text
\end{solution}
\end{questions}
\end{document}
感謝您的任何幫助,您可以提供。高拉夫
答案1
以下是我想到的一種可用於管理此類工作流程的方法。
您建立 3 個文件。第一個是您的主文件,tutorial.tex
例如:
\usepackage[T1]{fontenc}
\usepackage{pslatex}
\usepackage[pdftex]{color}
\usepackage[pdftex]{graphicx}
\begin{document}
\begin{questions}
\vskip 0.5 cm \question Question header \vskip 0.5cm
Question text
\begin{solution}
Solution text
\end{solution}
\end{questions}
\end{document}
另外兩個是包裝紙。例如,這些可能是tutorialQuestions.tex
:
\documentclass{exam}
\input{tutorial}
和tutorialSolutions.tex
:
\documentclass{exam}
\printanswers
\input{tutorial}
然後您可以分別編譯tutorialSolutions.tex
和 ,tutorialQuestions.tex
而不會覆蓋其他版本。或者您可以使用腳本來為您管理此操作。 (如何做到這一點取決於您的作業系統。)也可以使用 TeX 的各種幫助程式來完成許多工作和/或讓您的 IDE 自動化操作。然而,以上是基本思想,您可以將其嵌入到最適合您首選工具的方式中。
答案2
儘管CFR的回答非常好,也很籠統,我也遇到了這個問題,想使用一個IDE提出一個具體的解決方案,文字工作室。
為了實現這一點,我定義了一個使用者命令(Preferences > Build > User Commands
) 作為
"/path/to/script/compile-exam.py" %.tex -f | txs:///pdflatex | mv %.pdf %Solutions.pdf | mv %.synctex.gz %Solutions.synctex.gz | "/path/to/script/compile-exam.py" %.tex | txs:///pdflatex | txs:///view-pdf-internal "?m)Solutions.pdf"
compile-exam.py
我寫的Python腳本在哪裡(可用這裡):
from argparse import ArgumentParser
import re
from shutil import copyfile
parser = ArgumentParser()
parser.add_argument('filename')
parser.add_argument('-f', '--forward', action='store_true')
args = parser.parse_args()
copyfile(args.filename, args.filename+'.bak')
basename = re.match(r".+(?=\.tex)", args.filename).group(0)
if args.forward:
contents = []
with open(args.filename, 'r') as in_file:
for line in in_file:
if r'\printanswers' in line:
contents.append('\printanswers\n')
else:
contents.append(line)
with open(args.filename, 'w') as out_file:
for line in contents:
out_file.write(line)
else:
contents = []
with open(args.filename, 'r') as in_file:
for line in in_file:
if r'\printanswers' in line:
contents.append('%\printanswers\n')
else:
contents.append(line)
with open(args.filename, 'w') as out_file:
for line in contents:
out_file.write(line)
從這裡,我定義了一個鍵盤快速鍵來執行命令 ( Preferences > Shortcuts > Tools > User
)
使用此鍵盤快捷鍵,編譯器將產生exam1.pdf
並exam1Solutions.pdf
在內建檢視器中顯示解決方案檔案(仍然能夠滾動到上次編輯的位置等)。
另請注意,您需要python
先呼叫或使腳本可執行並#!/usr/bin/python
在第一行中添加類似內容。
這顯然不是一個通用的解決方案,並且確實需要調用外部程序,但它運行良好並且在合理的時間內運行。