(I'm using ConTeXt, but I suspect the answer will lie in plain TeX.)

I've created an environment that takes one argument myBufferName, and stores its contents in a buffer of that name. Unfortunately, all spaces after the argument are gobbled, and I don't know how to stop this properly. The usual gobble-stoppers, {} and \, end up in the buffer verbatim if I put them after \dostartbuffer.

    -- Print contents of buffer b to stdout and tmp.txt for inspection
    function printbuffer(b)
        -- Once to stdout ...

        -- ... and once to tmp.txt.
        jan = io.open('tmp.txt', 'w')

% This environment stores its contents in a custom-named buffer.
% (The #1 merely allows whitespace, like this: `\startMyBuffer [name]`)
    {% Store the name --- we'll need it when defining stopMyBuffer
     % ConTeXt macro that defines the \start... and \stop... commands.

%% % Hack I currently use: a dot at the end of the definition.
%% \def\startMyBuffer#1[#2]
%%     {\def\myBufferName{#2}
%%      \dostartbuffer[#2][startMyBuffer][stopMyBuffer].}
%% %                                                   ^
%% %                                                   |
%% % I later remove the dot using Lua.

% On closing the buffer, print its contents.

% Test it. You'll see the first line loses its leading whitespace :-(
    Andra moi ennepe,

When you try running it, this gives

Andra moi ennepe,

as the buffer's contents.


You define the macro as


To avoid the macro eating spaces, use


(Note the % at the end. You also have a trailing whitespace at the end of the next line. Thus, a proper definition of the macro is

    {% Store the name --- we'll need it when defining stopMyBuffer
     \def\myBufferName{#2}% <<<<---- ADDED THS
     % ConTeXt macro that defines the \start... and \stop... commands.

If you don't want to worry about all this, use \starttexdefinition ... \stoptexdefinition instead.

\starttexdefinition startMyBuffer #1[#2]
   % Store the name --- we'll need it when defining stopMyBuffer
   % ConTeXt macro that defines the \start... and \stop... commands.
| improve this answer | |
  • Tried them both, they both work a treat. Thank you! – Esteis Feb 19 '12 at 22:04

The general syntax of \def is

\def<name><parameter text>{<replacement text>}

, where <parameter text> is matched against input. The parameter text can contain parameter tokens (#1, #2, …) and other tokens, for example, #1[#2]abc. Other tokens are matched exactly against input — in this example, if TeX fails to find a sequence <something>[<something>]abc, it will stop with an error. After a match has been found, the <something> parts will be taken as macro parameters per parameter text definition, but the other matched tokens will be simply eaten. In your case the parameter text of \startMyBuffer contains space after [#2], thus it will be also matched and eaten.

Look at the output of this Plain TeX document to see the difference:




Conclusion: don't put extraneous symbols in macro parameter text. For Plain TeX/LaTeX, let { follow immediately after all parameters have been specified; for ConTeXt, put a % after the parameter text to ignore the newline and the following space.

| improve this answer | |
  • 1
    In ConTeXt, the standard style is to add a % on the \def line and put { on the next line. – Aditya Feb 15 '12 at 17:21

I'm not sure if I understand your question correctly, but you seem to indicate that you don't want TeX to gobble whitespace. If that's what you're after, check out the commands \obeyspaces (Plain TeX) and \obeywhitespace (eTeX).

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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