# How to define commands that do not consume any space

I want to create an environment that generates special tables and graphs. Inside the environment the user should be able to execute predefined commands that either modify the behavior of the final drawing or add elements that should be drawn.

The problem I am currently facing is that each command that is executed consumes some horizontal space even though it isn't outputting anything.

Is there any way to define commands that do not have any impact to the output at all?

Consider the following example:

\documentclass{article}

\newenvironment{test}{x}{y}
\newcounter{somecounter}
\newcommand{\somecommand}[1]{%
\setcounter{somecounter}{#1}%
}

\begin{document}

\section*{Test 1}
\begin{test}%
\somecommand{42}
\end{test}

\section*{Test 2}
\begin{test}
\end{test}
\end{document}


This is how the result looks like:

What I want is to define the command somecommand in a way that Test 1 would look just like Test 2. Is there any way to achieve this? Maybe I could put the command in an invisible box of or box with the height and width of 0?

If that can't be done, is there a way to calculate how much space a command would need, so that I could (programmatically) insert a negative horizontal and vertical space?

• This is the old problem of missing end-of-line %. A linebreak introduces a space, so there are two spaces in the definition of your macro, and one in the use of the environment. See the questions linked to in meta.tex.stackexchange.com/a/2429/586 for details. – Torbjørn T. Aug 25 '13 at 12:09
• @TorbjørnT. Thank you for the link, that was new to me. Nevertheless, If I add a '%' at the end of even each line on my example, the result still stays the same. Any idea how I could fix that simple example? – muffel Aug 25 '13 at 12:48
• Uh, no it doesn't. Not for me at least. Add a % after \newcommand{\somecommand}[1]{, and one after \setcounter{somecounter}{#1}. That takes care of the two in the macro definition. To remove the last, add a % after \begin{test} in Test 1. If you do that, do you still get exactly the same in both tests? – Torbjørn T. Aug 25 '13 at 13:07
• @TorbjørnT. I modified the code in the question just as you proposed. Unfortunately I still get the same result. – muffel Aug 25 '13 at 13:13
• Really? Then I have no idea what's going on, I get the same result for both cases. – Torbjørn T. Aug 25 '13 at 13:15

In this instance, you should define test to be

\newenvironment{test}{x\ignorespaces}{\unskip y}


which will remove (ignore) spaces after \begin{test} and gobble spaces before \end{test}. Also, you need to remove the spurious spaces in \somecommand:

\newcommand{\somecommand}[1]{%
\setcounter{somecounter}{#1}%
}


As reference, see What is the use of percent signs (%) at the end of lines?.

\documentclass{article}

\newenvironment{test}{x\ignorespaces}{\unskip y}
\newcounter{somecounter}
\newcommand{\somecommand}[1]{%
\setcounter{somecounter}{#1}%
}

\begin{document}

\section*{Test 1}
\begin{test}
\somecommand{42}
\end{test}

\section*{Test 2}
\begin{test}
\end{test}
\end{document}

• this is clearly the answer, but it's also clear that the extra space comes from within the definition of \newenvironment. that really shouldn't happen, should it? – barbara beeton Aug 25 '13 at 13:17
• @barbarabeeton: I guess. My assumption is that environments are less often used in an inline (or horizontal) form, but rather to set larger (or vertical) content. – Werner Aug 25 '13 at 13:21
• while it's undoubtedly true that environments are rarely used in horizontal mode, seems to me that this is a reasonable source of puzzlement. there's no bug report on this, although there was a report (latex/3106) in the latex bugs database about the extra space left in a following non-paragraph line. seems to me this is worth a report -- would you care to enter it? – barbara beeton Aug 25 '13 at 13:44
• @barbarabeeton why do you think it's a bug? – David Carlisle Aug 25 '13 at 13:51
• @DavidCarlisle -- i'm not sure it's a bug, but it's an infelicity that could use some explanation. maybe the answer is that environments are not meant to be used in horizontal mode, or they're not meant to be used with nothing between the \begin and \end. it would be useful to document that. (if it is documented -- and i haven't really looked just now -- i apologize, but think a reference would be worthwhile.) – barbara beeton Aug 25 '13 at 13:55