
我教授大學課程,我的目標是能夠pdf
從同一個tex
文件輸出兩個:
- 一個問題集
- 問題集及解決方案
我已經通過調用\newif
然後在標頭中設置\soltrue
或 來手動執行此操作\solfalse
,具體取決於我是否想要輸出解決方案。然後我將解決方案包含在\ifsol
和中\fi
,然後我們就開始比賽了。這很棒,因為對問題集的編輯只需要更改一個文檔,而不是兩個文檔。唯一的煩惱是我必須編譯一次,更改pdf
名稱,然後將標頭從 更改\soltrue
為\solfalse
,然後再次重新編譯以獲取兩個文檔。我想用arara
它來自動化。
我一直在關注這個優秀的如何創建yaml
規則來做arara
我想做的事。在操作方法中,作者 (cmhughes) 建立了一條規則,將tex
文件編譯為兩個pdf
,一個包含一列,另一個包含兩列。顯然我的應用程式略有不同。
我建立了以下solutions.yaml
規則:
!config
# Make two versions of document depending on if statement "sol"
# author: Shane Auerbach (based on work by Chris Hughes)
# requires arara 3.0+
#
# Sample usage
#
# % arara: solutions
# % arara: solutions: {solutions: true}
# % arara: solutions: {solutions: false}
#
identifier: solutions
name: Solutions
commands:
- <arara> pdflatex "\newif\ifsol\sol@{trigger}\input{@{file}}"
- <arara> @{ isWindows( "cmd /c move", "mv" ) } @{getBasename(file)}.pdf @{getBasename(file)}@{trigger.toUpperCase()}.pdf
arguments:
- identifier: trigger
flag: <arara> @{parameters.trigger}
default: false
這是測試mytex.tex
文件:
% arara: solutions: {trigger: true}
% arara: solutions: {trigger: false}
\documentclass{article}
\begin{document}
Question: What is $2+2$?
\ifsol \begin{quote} \textbf{Solution:} $2+2=3$ \end{quote} \fi
\end{document}
效果很好。當我運行arara
該tex
文件時,它會根據需要輸出兩個pdf
',一個包含解決方案,另一個不包含解決方案。我想做但不知道如何實施的兩個改進:
按原樣,我目前只能編譯該
tex
文件,arara
因為文件本身既沒有\soltrue
也\solfalse
沒有 。我希望能夠放入一些設定它的東西,當且僅當它尚未設定時,arara
以便我可以在外部編譯它arara
。但我不希望它覆蓋arara
的設置,因為這會達不到目的。有任何想法嗎?目前
pdf
產生的文件稱為mytexTRUE.pdf
(含解決方案)和mytexFALSE.pdf
(不含解決方案)。我更喜歡mytexS.pdf
(有解決方案)和mytexQ.pdf
(沒有解決方案)之類的東西。我認為實現這一點的唯一方法是在 中使用條件語句solutions.yaml
,但從我讀到的內容來看,yaml
它並不是真正為條件語句設計的。這方面有什麼想法嗎?
如果您已經讀到這裡,那麼您就是一個真正的英雄。謝謝!如果你跳過了中間部分,TL;博士:幫助這個陌生人使他的工作流程更有效率。
答案1
實現第一個改進的一種可能方法是利用而不是像原始程式碼那樣\ifdefined
依賴 a 。\newif
讓我們看看新的 TeX 程式碼:
\documentclass{article}
\begin{document}
Question: What is $2+2$?
\ifdefined\solutionflag\begin{quote} \textbf{Solution:} $2+2=3$ \end{quote}\fi
\end{document}
這裡的計劃很簡單:如果\solutionflag
定義了,則將處理特定的程式碼摘錄。這個想法與 using 非常相似\if<flag>
,但如果沒有設置這個裝置,它不會抱怨。:)
前面的程式碼可以使用您喜歡的引擎正常編譯,並且不需要arara
.如果您想列印答案,只需\def\solutionflag{}
在序言中寫下即可,我們就完成了。
更新:這個答案已更新為 4.0+ 版本的規則。檢查舊版的編輯歷史記錄。
讓我們看看這個改進的規則:
!config
identifier: solutions
name: Solutions
authors:
- A duck
commands:
- name: The engine
command: >
@{
prefix = isTrue(solutions, '\\def\\solutionflag{}', '');
input = '\\input{' + reference.getName() + '}';
return getCommand(engine, prefix + input, options);
}
- name: The renaming procedure
command: >
@{
separator = java.io.File.separator;
prefix = [];
if (isUnix()) {
prefix = [ 'mv' ];
}
else {
prefix = [ 'cmd', '/c', 'move' ];
}
parent = reference.getParent();
input = parent + separator + getBasename(reference) + '.pdf';
output = parent + separator + name + '.pdf';
return getCommand(prefix, input, output);
}
arguments:
- identifier: engine
flag: >
@{
if ([ 'pdflatex', 'latex', 'xelatex',
'lualatex' ].contains(parameters.engine)) {
return parameters.engine;
}
else {
throwError('The provided engine is not valid');
}
}
default: pdflatex
- identifier: name
flag: >
@{
return parameters.name;
}
required: true
- identifier: solutions
flag: >
@{
return isTrue(parameters.solutions);
}
default: >
@{
return false
}
- identifier: options
flag: >
@{
if (isList(parameters.options)) {
return parameters.options;
}
else {
throwError('I was expecting a list of options.');
}
}
規則參數:
engine
:字串,可選,設定引擎。可能的選項有pdflatex
、latex
、xelatex
和lualatex
。預設:pdflatex
.solutions
:自然布林值,可選,設定是否在文件範圍中定義解決方案巨集。預設:false
.name
:字串,必需,設定文件名稱。該文件將被重新命名為該值。無需指定.pdf
擴展名。options
:列表,可選,設定要提供給引擎的附加命令列參數列表。
範例執行:
% arara: solutions: { solutions: true, name: 'answers' }
% arara: solutions: { solutions: false, name: 'questions' }
\documentclass{article}
\begin{document}
Question: What is $2+2$?
\ifdefined\solutionflag\begin{quote} \textbf{Solution:} $2+2=3$ \end{quote}\fi
\end{document}
當運行它時arara
:
[paulo@cambridge ~] $ $ arara test.tex
__ _ _ __ __ _ _ __ __ _
/ _` | '__/ _` | '__/ _` |
| (_| | | | (_| | | | (_| |
\__,_|_| \__,_|_| \__,_|
Processing 'test.tex' (size: 264 bytes, last modified: 03/22/2020
06:54:54), please wait.
(Solutions) The engine .................................. SUCCESS
(Solutions) The renaming procedure ...................... SUCCESS
(Solutions) The engine .................................. SUCCESS
(Solutions) The renaming procedure ...................... SUCCESS
Total: 1.25 seconds
我們得到以下輸出:
[paulo@cambridge ~] $ ls *.pdf
answers.pdf questions.pdf
另一個有趣的建議是透過創建一個選擇性地顯示解決方案的環境來改進文件語義。以下程式碼是 David Carlisle 善意建議的(我欠他 5 美元):
\newenvironment{solution}{\ifdefined\solutionflag\else\setbox0\vbox\fi\bgroup}{\par\egroup}
希望能幫助你!:)