
学生向けにチュートリアルをいくつか書きたいのですが、そのためにexam
ドキュメント クラスとsolution
環境を使用しています。
問題は私が声明を出していることです
\printanswers
チュートリアルの場合はコメントを追加し、解決策の場合はコメントを解除する必要があります。
一度コンパイルしてソースファイル(たとえばtutorial.tex)から2つのPDFを作成できる方法があればいいのですが。
- tutorial.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}
ご協力いただければ幸いです。 gaurav
答え1
このタイプのワークフローを管理するために使用できる 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}
他の 2 つはラッパーです。たとえば、次のようになりますtutorialQuestions.tex
。
\documentclass{exam}
\input{tutorial}
そしてtutorialSolutions.tex
:
\documentclass{exam}
\printanswers
\input{tutorial}
その後、他のバージョンを上書きせずに別々にコンパイルできます。または、スクリプトを使用してこれを管理することもできます。(その方法は OS によって異なります。) TeXtutorialSolutions.tex
のtutorialQuestions.tex
さまざまなヘルパーを使用して、この作業の多くを実行したり、IDE で自動化したりすることもできます。ただし、上記は基本的な考え方であり、好みのツールに最適な方法で組み込むことができます。
答え2
その間cfrの回答は非常に素晴らしく、一般的です。私もこの問題に遭遇し、1つの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
最初の行に次のような内容を追加する必要があることに注意してください。
これは明らかに一般的な解決策ではなく、外部プログラムを呼び出す必要がありますが、適切に機能し、妥当な時間内に動作します。