3

I am defining a command that uses an subscript somewhere. I want the subscript to dissappear when I leave its corresponding command-input open, but then LaTeX will trow an error since no character was put in the subscript. Now I want to type a dummy character after the input to ensure that at least one character is printed at all times. However, I don't want this dummy character to show up. It just needs to be there to decieve LaTeX.

If I type \infty, I get an infinity symbol, if I print the character I am asking for, LaTeX prints a character that has no black.


In this code, the command \spacechar takes the role of the character I want

\newcommand{\tup}[3]{(-\; #1 -\! \underset{\! #2 \,\,\,}{)_{#3\spacechar}}}

instead of

\newcommand{\tup}[3]{(-\; #1 -\! \underset{\! #2 \,\,\,}{)_{#3}}}

which breaks when #3 is empty.

Some example of the code in action

\documentclass{article}
\usepackage{amsmath}
\usepackage{mathtools}
\newcommand{\tup}[3]{(-\; #1 -\! \underset{\! #2 \,\,\,}{)_{#3}}}
\begin{document}
\noindent $\tup{x_i}{i}{n}$\\
$(-\; x_i -\! \underset{\!  \,\,\,}{)}$
\end{document}  

Versions of the command


Something like this is also useful in many other places where LaTeX, such as when LaTeX wants to remove whitespace at the beginning of a sentence.

This is a slightly related but unsatisfying answer. Whitespace as wide as a character It gives whitespace commands, which are not actual characters. They are whitespace, and whitespace can't decieve LaTeX.

This is also not what I'm looking for. Explicit space character? Instead of invisible characters it gives me visible whitespace.

In short, I want a character that prints absolutely nothing.

4 Answers4

3

Your problem in using \underset is that it tries to center the underset material. You want the underset to be left aligned with the paren, if I understand your intentions. To do that, I use a left-aligned stack.

Here, I show the 4 cases where #2 and #3 are either present or absent.

\documentclass{article}
\usepackage{stackengine}
\usepackage{mathtools}
\newcommand{\tup}[3]{(-\; #1 -\! 
  \ensurestackMath{\stackengine{1pt}{)_{#3}}{\scriptstyle#2}{U}{l}{F}{F}{S}}}
\begin{document}
\noindent $\tup{x_i}{i}{n}$\\[4pt]
$\tup{x_i}{i}{}$\\[4pt]
$\tup{x_i}{}{n}$\\[4pt]
$\tup{x_i}{}{}$
\end{document} 

enter image description here

2

There is no need for a “\spacechar”.

\documentclass{article}
\usepackage{amsmath}
\usepackage{mathtools}

\newcommand{\tup}[3]{%
  ({-}\; #1 \;{-}%
  \mathclose{\underset{\mathclap{#2\,}}{)}}%
  \if\relax\detokenize{#3}\relax\else
    \mathclose{}_{#3}%
  \fi
}

\begin{document}

$\tup{x_i}{i}{n}$

$\tup{x_i}{i}{}$

\end{document}

With \underset{\mathclap{#1\,}{)} we set the argument below the parentheses, making the whole construction a Close atom; if the third argument is not empty, a phantom parentheses receives the subscript.

enter image description here

I also propose a friendlier syntax, where the subscript is specified with the standard _:

\documentclass{article}
\usepackage{amsmath}
\usepackage{mathtools}
\usepackage{xparse}

\NewDocumentCommand{\tup}{mme{_}}{%
  ({-}\; #1 \;{-}
  \mathclose{\underset{\mathclap{#2\,}}{)}}%
  \IfValueT{#3}{\mathclose{\vphantom)}_{#3}}%
}


\begin{document}

$\tup{x_i}{i}_{n}$

$\tup{x_i}{i}$

\end{document}

Similarly, but with also the second argument optional, in case you sometimes need no variable below the parenthesis.

\documentclass{article}
\usepackage{amsmath}
\usepackage{mathtools}
\usepackage{xparse}

\NewDocumentCommand{\tup}{mme{_}}{%
  ({-}\; #1 \;{-}
  \mathclose{\underset{\mathclap{#2\,}}{)}}%
  \IfValueT{#3}{\mathclose{\vphantom)}_{#3}}%
}


\begin{document}

$\tup{x_i}{i}_{n}$

$\tup{x_i}{i}$

\end{document}

enter image description here

egreg
  • 1,121,712
0

The definition in the question is correct and does not break. I presume I made forgot the braces in the actual file. The above answer from @Steven B. Segletes does make the command better.

However, as for my question on invisible characters, I have found an imperfect solution.

\usepackage{xcolor}
\newcommand{\spacechar}{\text{\textcolor{white}{\tiny .}}}

This will print a tiny white dot, which works so long as the background isn't white.

  • 2
    why would you ever want a coloured . ? not say a thin space like \, or nothing? I don't think this is really an answer to the question as asked. If you turned on tracing to see how much work tex does to evaluate \text{\textcolor{white}{\tiny .} you would see several hundred lines, \text evaluates its argument four times, and \tiny loads an entire font setup, \textcolor inserts additional colour whatsits.... – David Carlisle Oct 18 '19 at 16:12
  • @DavidCarlisle why \text evaluates its argument four times? I don't understand. Thank you! – manooooh Oct 19 '19 at 05:52
  • @manooooh https://tex.stackexchange.com/a/110637/1090 – David Carlisle Oct 19 '19 at 07:07
  • @DavidCarlisle I still do not understand. What does \mathchoice command have to do with \text? – manooooh Oct 27 '19 at 01:05
  • 1
    @manooooh the difference between mbox and text is just that text uses mathchoice so it is smaller in subscripts – David Carlisle Oct 27 '19 at 08:17
0

With xparse optional arguments:

\documentclass{article}
\usepackage{amsmath}
\usepackage{mathtools}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand \otup { m O{} O{} }{
    (-\; #1 -\! \underset{\tl_if_empty:nTF{#3}{}{\!} #2 \,\,\,}{\tl_if_empty:nTF{#3}{\!\!}{})\sb{#3}}
}
\ExplSyntaxOff
\begin{document}
$\otup{x_i}[i][n]$

$\otup{x_i}[i]$

$\otup{x_i}$

\end{document}  

This does not use the stackengine solution of the other answer, instead when the 3rd argument is empty (not given) then the right parenthesis is shifted back a bit, if the argument is not empty then the underset argumentitself (#2) is shifted left.

Note that with \ExplSyntaxOn the _ character is no longer a math subscript, so \sb is used instead.

enter image description here

Marijn
  • 37,699