2

I'm currently writing a small tool which automatically generates latex code for equations. I stumbled over the following problem, which in extremely boiled-down form looks like this:

\begin{align*}
a
&=
\left( \left[ \left\{ \left( b
\vphantom{x} \right.
\vphantom{x} \right.
\vphantom{x} \right.
\vphantom{x} \right. 
\\&\quad\;
%--------
c
\\&\quad\;
%--------
\left. \vphantom{x}
\left. \vphantom{x}
\left. \vphantom{x}
\left. \vphantom{x}
d
\right) \right\} \right] \right)
\\&\quad\;
%--------
\left. \left. \left. \left.
e
\right) \right\} \right] \right)
\end{align*}

The output contains some space for which I have no explanation:

enter image description here

Please let me guide you through this.

In the align environment, I have a left side (a) only in the first line, and right sides in all lines. I insert a \quad\; at the beginning of each line (except for the first) to jump over the space of the = character.

In the first line (a, b), I have four opening brackets with auto-scaling (\left). The \vphantom are required in my code to keep the vertical size of the brackets over the line breaks; they contain more complex text, but here I simply pass x. I compensate the four opening brackets with for virtual closing brackets (\right.).

In the second line (c), I just have c which nicely aligns below the right side of the first line. This is what I want for the third line as well.

In the third line (d) (which is actually of the form that my code generates), I have four virtual opening brackets (\left.) and four closing brackets. Now this line generates a lot of space before the d.

In the fourth line (e) I deleted all the \vphantom commands. Surprisingly, this reduces the space. The remaining space seems to be generated by the \left. commands. I could understand if this would be the space occupied by the closing brackets, but it is smaller (please compare with the first line).

So, my problem is that I want to compensate for the unwanted space in the third line by inserting a negative space (e.g. \!), but I have no clue how and why latex generates the space in the first place. Since the code is automatically generated and has an arbitrary number of brackets, I can't just use a negative space of fixed size.

I there anyone deeply familiar with the internal latex code to tell me what latex is doing in this case, such that I can compensate for the unwanted space? Thanks a lot!


Edit in response to campa: I included \setlength{\nulldelimiterspace}{0pt} which seems to reduce the spacing, but doesn't eliminate it completely (second example):

enter image description here

5
  • Null delimiters (can) have a non-zero width controlled by the primitive register \nulldelimiterspace (the default is something more than 1pt IIRC). – campa Apr 11 at 14:41
  • @campa: Thanks a lot. Setting length \nulldelimiterspace to zero actually reduces the space, but doesn't eliminate it. I edited my answer. – Ralf Apr 11 at 15:13
  • TeX inserts a thin space between an ordinary atom and an inner atom. So there is a thin space between \vphantom{x} and \left. Besides \nulldelimiterspace, this is another reason for not using indiscriminately \left and \right. – egreg Apr 11 at 16:14
  • @egreg: Thanks a lot. Is there a length variable connected to this thin space (which could be set to zero)? – Ralf Apr 11 at 17:51
  • @Ralf Yes, but you’d end up ruining several other parts. – egreg Apr 11 at 19:35
2

I think you need to run \setlength\nulldelimiterspace{0pt}, load the mleftright package, and replace all instances of \left and \right with \mleft and \mright, respectively.

Separately, I'd replace all instances of &\quad\; with &\phantom{{}={}} in order to guarantee correct alignment.

In the following screenshot, I employ | merely to indicate the start and end of material on the right hand side of the (visible or hidden) = symbols. Observe that I've also dropped all \vphantom{x} directives since they don't do anything.

enter image description here

\documentclass{article}
\usepackage{amsmath}    % for 'align*' environment
\usepackage{mleftright} % for '\mleft' and '\mright' macros
\begin{document}
\setlength\nulldelimiterspace{0pt}
\begin{align*}
a
&=
|\mleft( \mleft[ \mleft\{ \mleft( 
b
\mright. \mright. \mright. \mright. | \\
&\phantom{{}={}}
|b| \\
&\phantom{{}={}}
|\mleft. \mleft. \mleft. \mleft. 
b
\mright) \mright\} \mright] \mright) | 
\end{align*}
\end{document}
1
  • 2
    Perfect, that works, thanks a lot, that is really helpful! – Ralf Apr 11 at 18:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.