6

In my overkill solution to Enclose an entry in an enumerate list in parentheses there is spurious space at the beginning of some (not all) uses of \SpecialItem. I have narrowed it down to the MWE below where the macro \SpecialItem is defined with two optional first parameters, the first of which defaults to \DrawLines:

\NewDocumentCommand{\SpecialItem}{O{\DrawLines} o}{...code...}

In the \SpecialItem macro defined here, the first optional parameter is not used (the macro \DrawLines is not even defined). So, using:

\SpecialItem  ...text...

is just fine. But if I specify the first optional parameter:

\SpecialItem[\DrawLines]  ...text...

I get spurious space introduced following the \item's label as in the third list item:

enter image description here

Notes:

  • If \tikzmark is not used, or defined to be {}, then there is no spurious space. I don't think this problem is related to \tikzmark as I have used that numerous time and not noticed this problem earlier.

  • Moving the \tikzmark to be before the \item also does not produce this spurious space. But, I need it to be after the label as I want a reference to the start of this \item, and not to the end of the previous \item.

  • In this answer to strange interaction between mdframed and item, egreg mentions that

    Redefining \item can be dangerous and have impredictable results

    Even though in the actual usage I am redefining \item, in this MWE \item is not redefined, just called via another macro. Hence, I do not think that the problem is related to this.

Code:

\documentclass{article}
\usepackage{xparse}
\usepackage{tikz}
\usepackage{showframe}

\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node (#1) {};}
%\renewcommand{\tikzmark}[1]{}%  Using this does not produce spurious space

\NewDocumentCommand{\SpecialItem}{O{\DrawLines} o}{%
    % #1 is not really used in this MWE, but is used in actual application
    \IfNoValueTF{#2}{%
        \item\tikzmark{MarkStart}%
    }{%
        \item[#2]\tikzmark{MarkStart}%
    }%
}%


\begin{document}
\begin{enumerate}
\item item
\SpecialItem SpecialItem without optional param
\SpecialItem[\DrawLines] SpecialItem with optional param
\item item
\end{enumerate}
\end{document}
0

1 Answer 1

7

The usual \item command doesn't start horizontal mode, so a space after the closing ] of its optional argument is ignored.

The problem with your macro is that \item is followed by \tikzmark which calls \tikz which in turn starts horizontal mode. Now the space after ] in

\SpecialItem[\DrawLines] SpecialItem with optional param

is not ignored any more.

Solution

Add \ignorespaces:

\NewDocumentCommand{\SpecialItem}{O{\DrawLines} o}{%
    % #1 is not really used in this MWE, but is used in actual application
    \IfNoValueTF{#2}{%
        \item\tikzmark{MarkStart}%
    }{%
        \item[#2]\tikzmark{MarkStart}%
    }%
    \ignorespaces
}

You must log in to answer this question.

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