TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm working on building an expenses package. The intention is to be able to write

\expense{someone}{something}{something a bit longer}{10}{GBP}
\expense{someone else }{something else}{something a bit longer}{11}{EUR}

To do this, I am starting an internal \allexpenses macro


Then each \expense command appends to that a \writeexpense command defined as

    #1 & #2 & #4 \\ %

So that eventually the \makeexpensetable command will simply begin a table, then have an \allexpenses line containing all the rows and end the table.

My current \expense command is


However, TeX is throwing errors due to the presence of the \writeexpense command inside. How do I stop that command from expanding? I'm guessing the answer has something to do with the correct use of a \noexpand command, but I cannot figure it out.

share|improve this question
Welcome to TeX.sx! Usually, we don't put a greeting or a “thank you” in our posts. While this might seem strange at first, it is not a sign of lack of politeness, but rather part of our trying to keep everything very concise. Accepting and upvoting answers is the preferred way here to say “thank you” to users who helped you. – Jubobs Apr 10 '13 at 12:32
up vote 6 down vote accepted

Alternative, @-free solution:


There are also one or two package-free solutions, but they effectively reinvent the \appto wheel. The problem is that \edef expands its contents recursively, and you only want the initial \allexpenses to be expanded once. In fact, it's not \noexpand but \unexpanded that you want:


This causes \allexpenses to be expanded once into whatever it contains, and then those contents to be protected from further expansion. The first line is equivalent to \expandonce\allexpenses, where \expandonce is also defined in etoolbox. The eTeX-free solution is to use a token register for this:

\toks0=\expandafter{\allexpenses \writeexpense{#1}{#2}{#3}{#4}{#5}}

Token registers were the original \expandonce, but since their assignment is not fully expandable, they are not always suitable where this eTeX-based macro is.

share|improve this answer
The first \expandafter in the second solution is redundant. The third solution is wrong because \writeexpense would be expanded! Just put it in \toks0 beforehand. – egreg Apr 10 '13 at 14:10
Both the second and third solution would edef-expand \writeexpense and all its arguments, meaning no fragile commands (such as accented letters) could be used in the arguments – David Carlisle Apr 10 '13 at 14:42
Fixed, @both of you. – Ryan Reich Apr 10 '13 at 14:44
Also some of us like solutions with as many @ as possible. – David Carlisle Apr 10 '13 at 14:47
I put one in my comment just for you :) – Ryan Reich Apr 10 '13 at 14:48

use \g@addto@macro\allexpenses{\writeexpense{#1}{#2}{#3}{#4}{#5}}
in a package file or between \makeatletter \makeatother

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.