I'm certain my problem is due to my almost complete lack of understanding of LaTeX's expansion rules...
I wrote some code, which uses the SageTeX package to keep track of a list of answers to homework problems and then print them all at the end. Here is an example of part of it:
\begin{sagesilent}
class AnswerList:
def __init__(this):
this.answers = []
def _latex_(this):
result = "\\begin{answers}\n"
for answer in this.answers:
result += " \\item " + str(answer) + "\n"
result += "\\end{answers}\n"
return result
def next(this, answer):
this.answers.append(answer)
answerlist = AnswerList()
\end{sagesilent}
This compiles in my LaTeX document just fine. I then use other SageTeX commands to add answers (in order) to this object, and then print them all at the end automatically, and it produces the desired results. However, I was hoping to move all the programming into macros in a separate file to make it more reusable and to keep my LaTeX document clean. So, one macro I created was:
\newcommand{\makeanswers}{%
\begin{sagesilent}
class AnswerList:
def __init__(this):
this.answers = []
def _latex_(this):
result = "\\begin{answers}\n"
for answer in this.answers:
result += " \\item " + str(answer) + "\n"
result += "\\end{answers}\n"
return result
def next(this, answer):
this.answers.append(answer)
answerlist = AnswerList()
\end{sagesilent}
}
I then include the second document into my first document (with an \input
command) and call \makeanswers
right after I enter the document environment in my main LaTeX file. I'm getting runaway argument errors when I try to compile the document.
Is there a way to create a macro that inserts this chunk of LaTeX code? There is probably a more LaTeX-friendly, less Python-dependent, approach to my problem here, but it is what I know...