1

Today i came across a strange issue when using color in a self defined command with one optional argument. When i use the command with the optional argument an indent happens for the following paragraph while no indent appears when not using an argument.

MWE:

\documentclass{minimal}

\usepackage{color}

\newcommand{\heading}[1][]{%
    {\color{blue}B\\}%
    % {B\\}%
}%

\begin{document}
\heading[ABCD]
Hello Darkness my old friend. Hello Darkness my old friend. Hello Darkness my old friend. Hello Darkness my old friend. Hello Darkness my old friend. Hello Darkness my old friend. Hello Darkness my old friend. Hello Darkness my old friend. Hello Darkness my old friend. Hello Darkness my old friend. Hello Darkness my old friend. Hello Darkness my old friend. Hello Darkness my old friend. Hello Darkness my old friend. Hello Darkness my old friend. Hello Darkness my old friend. Hello Darkness my old friend.

\heading
Hello Darkness my old friend. Hello Darkness my old friend. Hello Darkness my old friend. Hello Darkness my old friend. Hello Darkness my old friend. Hello Darkness my old friend. Hello Darkness my old friend. Hello Darkness my old friend. Hello Darkness my old friend. Hello Darkness my old friend. Hello Darkness my old friend. Hello Darkness my old friend. Hello Darkness my old friend. Hello Darkness my old friend. Hello Darkness my old friend. Hello Darkness my old friend. Hello Darkness my old friend.
\end{document}

When line 5 ({\color{blue}B\\}) is used the resulting document looks like this: Using color in the command causes a indent

When using line 6 ({B\\}) the indent does not happen: No indent is happening when not using a color

Based on my research this happens regardless of using color or xcolor. Now i am wondering why this is happening in the first place (is it a bug?) and how to fix this behavior.

3
  • 1
    Add \ignorespaces at the end of your \heading command Oct 8, 2020 at 20:32
  • @PhelypeOleinik Thanks for your fast response, it now works as expected. Could you also explain why that happens in the first place?
    – CptPie
    Oct 8, 2020 at 20:39
  • You are adding a space after the heading, instead of ignorespaces you could do \heading[ABCD]Hello Darkness with no space before Hello. Oct 8, 2020 at 20:58

1 Answer 1

2

The behaviour you see can be narrowed down to

B\\ Hello

B\\\write-1{} Hello

the difference between the two lines being the \write-1{}. Which can be dissected even further to

B\nobreak\hfil\break
          \space Hello

B\nobreak\hfil\break
\write-1{}\space Hello

The above will make two very similar horizontal lists (a chunk of text that TeX hasn't yet broken into lines to make a paragraph). When TeX breaks the first horizontal list into lines, the \nobreak\hfil will fill the rest of the B line with blank space, and the \break will force a line break at that point. The \space will then be discarded because it happens at the beginning of the line, and Hello will be typeset flush with the left margin.

The difference in the second horizontal list is that it contains a \write, which adds a “whatsit” node at that point, which occurs at the beginning of the line, since it was broken by \break. The \space that follows is not discarded, because it happens in the middle of the line.

In your case, the “whatsit” is not a \write, but a \special, which is inserted there because the scope of the current colour ends and the color package uses that \special to revert back to the previous colour.


To work around that, you can either add \ignorespaces to your macro, so that blank spaces will be ignored:

\newcommand{\heading}[1][]{%
  {\color{blue}B\\}%
  \ignorespaces
}%

or you can use instead \textcolor{blue}{B}, so that instead of inserting the whatsit in the beginning of the next line, it will be inserted right after B and won't disturb your horizontal list. Or even better, use both:

\newcommand{\heading}[1][]{%
  \textcolor{blue}{B}\\%
  \ignorespaces
}%

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .