3

I have the following problem of spacing not being quite right:

1

This is produced by the following LaTex code:

\documentclass[a4paper, 12pt]{article}

\newcommand{\opens}[2][]{\restr{\mathcal{T}_{#2}}{#1}}
\newcommand{\restr}[2]{{\ifx\\#2\\ #1 \else #1|#2 \fi}}

\begin{document}

\begin{tabular}{ll}
Want this & Get this \\
$(\mathcal{T})$ & $(\opens{})$ \\
$(\mathcal{T}|X')$ & $(\opens[X']{})$ \\
$(\mathcal{T}_X|X')$ & $(\opens[X']{X})$ \\
\end{tabular}

\end{document}

The reason for the odd spacing seems to be that an empty subscript shrinks the space for the T. The macro \opens is meant to work in different conditions:

  • If the optional parameter is non-empty (e.g. X'), then (and only then) it should append that parameter after a bar (e.g. |X').
  • If the main parameter is given (e.g. X), it should be placed as a subscript for the T.

How can I make the \opens macro to work so that it produces the correct spacing in all cases?

2 Answers 2

4

You are right, if there is subscript then it is printed without italic correction and no italic correction is added. If there is no subscript then italic correction is added. You can solve your problem by:

\newcommand{\opens}[2][]{\restr{\mathcal{T}\ifx\\#2\\ \else_{#2}\fi}{#1}}
\newcommand{\restr}[2]{{\ifx\\#2\\ #1 \else #1|#2 \fi}}
1
  • Interesting. I did not know you can slice that subscript underscore away like that.
    – kaba
    Aug 10, 2022 at 8:30
2

Let's look at a simpler example:

\documentclass{article}
\usepackage{amsmath}

\pagestyle{empty}\showoutput

\begin{document}

$\mathcal{T})$

$\mathcal{T}_{X})$

$\mathcal{T}_{})$

\end{document}

The relevant information in the log file is

....\mathon
....\OMS/cmsy/m/n/10 T
....\kern2.54167
....\OT1/cmr/m/n/10 )
....\mathoff
[...]
....\mathon
....\OMS/cmsy/m/n/10 T
....\hbox(4.78334+0.0)x7.60211, shifted 1.49998
.....\OML/cmm/m/it/7 X
....\OT1/cmr/m/n/10 )
....\mathoff
[...]
....\mathon
....\OMS/cmsy/m/n/10 T
....\hbox(0.0+0.0)x0.5, shifted 1.49998
....\OT1/cmr/m/n/10 )
....\mathoff

What can we see? In the first formula, a kern appears between the T and the parenthesis: it's the italic correction that TeX adds in specific cases after letters in math mode. In the second formula, no such kern appears because there is a subscript. The width of the subscript already includes the \scriptspace; indeed, if we set \scriptspace to zero we get

....\mathon
....\OMS/cmsy/m/n/10 T
....\hbox(4.78334+0.0)x7.10211, shifted 1.49998
.....\OML/cmm/m/it/7 X
....\OT1/cmr/m/n/10 )
....\mathoff

because the standard value of \scriptspace is 0.5pt.

In the third formula, only the \scriptspace is added, because the subscript is there, although empty: it's a known trick in order to omit the italic correction, but in your case it trips you up.

You need to conditionally add the subscript only if it's not empty.

I'm not sure I'd adopt your syntax: the optional argument should be the possible subscript to T, in my opinion, so I'll provide solutions for both ways.

No \ifx\\#1\\; I propose the more robust \IfValue(TF) and \IfBlank(TF) instead.

\documentclass{article}
\usepackage{amsmath}

% your syntax
\NewDocumentCommand{\opens}{om}{%
  \mathcal{T}\IfBlankF{#2}{_{#2}}% base symbol
  \IfValueT{#1}{\,|\,#1}% restr
}

% my syntax
\NewDocumentCommand{\egregopens}{om}{%
  \mathcal{T}\IfValueT{#1}{_{#1}}%
  \IfBlankF{#2}{\,|\,#2}%
}

\begin{document}

Your syntax

$(\opens{})$

$(\opens[X']{})$

$(\opens[X']{X})$

\bigskip

My syntax

$(\egregopens{})$

$(\egregopens{X'})$

$(\egregopens[X]{X'})$

\end{document}

enter image description here

3
  • I'm having trouble building this code on pdflatex. I added \usepackage{xparse}, but even after that I have an error about presumably missing IfBlankF (no error from IfValueT).
    – kaba
    Aug 10, 2022 at 9:53
  • Updating MikTeX solved the problem. The xparse and those conditionals are really nice! I'll have to switch to use them instead of my archaic methods.
    – kaba
    Aug 10, 2022 at 10:11
  • @kaba Yes, you need an up-to-date LaTeX. You don't even need \usepackage{xparse} if you have the latest release.
    – egreg
    Aug 10, 2022 at 10:17

You must log in to answer this question.

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