(preface: this lua stuff is just the motivation the real issue is with pure LaTeX)
I'm currently in some package writing in particular with LuaLaTeX. Since I'm often having a/some commands which collect some data to store data in a table to print it later (after e.g. doing some sorting or grouping). Therefore these commands just invoke \directlua
and expand effectively to nothing.
Now when working with tables, I noticed that such commands produce extra space in a cell (even though the expand to nothing) if an optional argument is involved, see this example with some variations:
\documentclass{article}
\parindent0pt
\begin{document}
\newcommand{\myCmd}[2][]{}
With opt arg:\\
\begin{tabular}{|l|}
x \\
\myCmd{x}
\myCmd{x}
\myCmd{x}
x \\
\end{tabular}
\vspace{.5cm}
\renewcommand{\myCmd}[1]{}
Without opt arg:\\
\begin{tabular}{|l|}
x \\
\myCmd{x}
\myCmd{x}
\myCmd{x}
x \\
\end{tabular}
\vspace{.5cm}
\renewcommand{\myCmd}[2][]{}
Outside of a table with opt arg:\\
x \\
\myCmd{x}
\myCmd{x}
\myCmd{x}
x \\
\vspace{.5cm}
\renewcommand{\myCmd}[1]{}
Outside of a table without opt arg:\\
x \\
\myCmd{x}
\myCmd{x}
\myCmd{x}
x \\
\end{document}
(compiled with pdflatex
, but lualatex
looks almost the same)
Now I for some part I know where the spacing comes from, since when I suffix the lines just containing the \myCmd
with a %
(or put the lines all in one line) the space is gone.
What I definitely don't understand why this corresponds to having optional arguments.
But most importantly my question is how to get rid of the spacing (in the best case without adding something like %
in the "normal" code since it would be nice to hide this in the implementation of my package).
\newcommand{\myCmd}[2][]{\ignorespaces}
.