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

It is easy to forget a \relax or % at the line end of commands in the body of macros. For example, in writing


You must add a space swallower at the end of the first and third line, but not at the end of the second, fourth and fifth lines.

A simple global search/replace of {$ by {% will fix omissions such as that of the first line, and is not likely to add redundant swallowers. But, a global search/replace of }$ by }% will also add a percent after the fifth line.

For the benefit of Google search visitors, let me write the vi commands for these searches:


But as I mention, these are not perfect, and may not take care of other tricky cases.

So, my question is: Can you do better? Perhaps there is a clever redefinition of \def that would eliminate the need for these ugly additions, or perhaps there is some other automatic mechanism that would warn against such potential bugs?

share|improve this question
You should mentioned that $ is the end-of-line marker in regular expressions used for the search&replace. Otherwise some people will wonder what you mean with replacing {$ with {%. – Martin Scharrer Feb 27 '11 at 19:58
I edited your question, because your original example with \invokeM1 is equivalent to \invokeM 1 (digits cannot be part of the macro name (with the default catcodes)) and therefore all lines would need to eat spaces. – Lev Bishop Feb 27 '11 at 20:14
up vote 2 down vote accepted

I wrote some code to check a chunk of code. It counts how many spaces and how many spaces+end-line-characters appear in the argument of \cnel, and displays it to the terminal. It also runs the code: this way, it shouldn't be too disruptive.

% Should print 1 and 4.
  \def\foo{b ar

% Just to stop the run (press <enter>)
\read-1 to\dummy

% Should print 0 and 0, because there is no space nor end-of-line.

The trick is to

  • use \scantokens to read the argument with various catcode setups for the end-of-line character: either ignored or space. We first need to read it with catcode other because "ignored" it would disappear, and "space", it would become indistinguishable from a true space.

  • use \lowercase to convert spaces to some other character (namely ^^S), then \detokenize, and search for the string ^^S. (That string is itself produced by detokenizing a lowercase space.)


% `cnel` = `check if no end-line`.

  % Set spaces to lowercase to the first, optional, argument (default ^^S).
  \lccode`\ `#1%
  % First we count the number of spaces with a setting in which 
  % the end-line character is ignored. Then we count it after setting
  % it to be a space instead.
  \typeout{First, we ignore end-of-line characters.}%
  \typeout{Then, they are counted as spaces.}%
  % To execute the code, we keep this endline=space setting.

  \lowercase{\expandafter\cnel@w\detokenize{. .#1 }\relax%
  \typeout{Number of space(s): \the\cnel@count.}%
      \advance \cnel@count by 1\relax%
  \expandafter\ifx\expandafter a\detokenize{#1}a%
share|improve this answer
Brilliant! I just cannot tell you how much I appreciate this. I think this should be packaged and advertised! – Yossi Gil Mar 12 '11 at 10:30
@Yossi: I'll try to understand the code again this or next week-end, and perhaps turn it into an environment, and think a bit more about catcodes. – Bruno Le Floch Mar 12 '11 at 16:02
@Bruno: I know this may be very time consuming. Should you decide to to do this, I trust the community will be very grateful. – Yossi Gil Mar 13 '11 at 13:50
@Yossi: it's not that time consuming. I just can't figure out a good interface. And I can't remember why I treated spaces and end nf line character differently above. If you have any clue, I'd appreciate the help. – Bruno Le Floch Mar 17 '11 at 10:09
@Yossi: I'm getting into unforseenly busy times, unfortunately. And my quick attempt at packaging tonight didn't work. So that will happen later. Sorry. --- About why eol are treated specially: the idea is that the user might want to ignore spaces which appear in the middle of the line, because those are more often intentional. Also I need to implement ignoring spaces outside braces. – Bruno Le Floch Mar 18 '11 at 23:55

ConTeXt provides \starttexdefinition ... \stoptexdefinition environment that does exactly that. Using that you can write your command as:

\starttexdefinition myMacro

I believe that LaTeX3 also provides a similar syntax.

share|improve this answer
Thanks Atidya. I applied Lev's correction to your answer as well. – Yossi Gil Feb 28 '11 at 3:51

For LaTeX, using the expl3 package:


or even

\def \myMacro {
  \invokeY { }
share|improve this answer
You could add that when the ExplSyntax is on, spaces are ignored, the end of line character is a space, ~ is a genuine space, and : and _ are letters (usable in macro names). (Am I missing something?) – Bruno Le Floch Feb 28 '11 at 10:58

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.