18

Suppose I need to get a formula like $L^{2,3/4,5/6}_{1,2,3}$ in Latex. Then $2$--$1$, $3/4$--$2$, $5/6$--$3$ is not aligned. See enter image description here Then how can I let these three to be aligned?

XLDD
  • 383
  • Welcome to TeX.sx! Usually, we don't put a greeting or a "thank you" in our posts. While this might seem strange at first, it is not a sign of lack of politeness, but rather part of our trying to keep everything very concise. Upvoting is the preferred way here to say "thank you" to users who helped you. – Przemysław Scherwentke Nov 01 '13 at 05:02
  • 2
    This just a notational crime against your readers! – percusse Nov 01 '13 at 11:02

6 Answers6

14

Let us make things complicated.

\documentclass{article}
\usepackage{calc}
\begin{document}
$L^{2,3/4,5/6}_{1,\makebox[\widthof{3/4}]{$\scriptstyle 2,$}\makebox[\widthof{5/6}]{$\scriptstyle 3\hphantom{,}$}}$
\end{document}

enter image description here

I agree with Benjamin's comments. Hence an attempt to increase the spaces.

\documentclass{article}
\usepackage{calc}
\begin{document}
$L^{2,3/4,5/6}_{1,\makebox[\widthof{3/4}]{$\scriptstyle 2,$}\makebox[\widthof{5/6}]{$\scriptstyle 3\hphantom{,}$}}$
\end{document}

enter image description here

One can also resort to reduce the space before and after / by using

\newcommand*{\mybar}{\kern-.2ex/\kern-.2ex}

instead of /.

  • I think it is exactly what the OP wanted, but there is more spacing around the $/$ than around the $,$s, so my eye doesn't immediately group the $3/4$ together. All in all, I think the OP has chosen a poor notation to start with. – Benjamin McKay Nov 01 '13 at 09:04
13

Just for fun, with low level commands:

\documentclass{article}
\newcommand{\stacked}[2]{%
  \vcenter{\tabskip=0pt
    \gdef\separator{/}
    \halign{$\scriptstyle##$\hfil&&$\scriptstyle\separator##$\hfil\cr
      #1\cr
      \noalign{\nointerlineskip\kern.2ex\gdef\separator{\phantom{/}}}
      #2\crcr
      \noalign{\kern.275ex}
    }%
  }%
}

\begin{document}
$L^{2/}_{1,}L\stacked{2,3&4,5&6}{1,&2,&3}$

\end{document}

enter image description here


If centered alignment is preferred, here's a (rather complicated) way.

\documentclass{article}
\usepackage{xparse}
\newlength{\commawidth}
\newcommand{\dostacked}[2]{%
  \settowidth\commawidth{$\scriptstyle,\,$}
  \vcenter{
    \tabskip=0pt
    \halign{\hfil$\scriptstyle##\vphantom{/}$\hfil\tabskip\commawidth
            &&\hfil$\scriptstyle##\vphantom{/}$\hfil\cr
      #1\cr
      \noalign{\nointerlineskip\kern.2ex}
      #2\crcr
      \noalign{\kern.275ex}
    }%
  }%
  \kern-\commawidth
}
\ExplSyntaxOn
\NewDocumentCommand{\nospacecomma}{}
 {
  \rlap{$\scriptstyle,$}
 }
\cs_set_eq:NN \egreg_dostacked:nn \dostacked
\cs_generate_variant:Nn \egreg_dostacked:nn { xx }
\NewDocumentCommand{\stacked}{mm}
 {
  \seq_set_split:Nnn \l_tmpa_seq { , } { #1 }
  \seq_set_split:Nnn \l_tmpb_seq { , } { #2 }
  \egreg_dostacked:xx
   { \seq_use:Nn \l_tmpa_seq { \nospacecomma& } }
   { \seq_use:Nn \l_tmpb_seq { \nospacecomma& } }
 }
\ExplSyntaxOff

\begin{document}
$L^{2/}_{1}L\stacked{2,3/4,5/6}{1,2,3}X$

\end{document}

The first L^{2/}{1} is just for comparing the heights, while the trailing $X$ is for checking that no space is added at the end.

Not the best code. But, hey, it works!

enter image description here

egreg
  • 1,121,712
8

Using the tabstackengine package introduced at Writing a table with equally spaced columns, based on the widest column, we make the sub/super array a stack, and only have to tweak the space between the sub/super line and the vertical placement of the subscript line. A long stackgap of 6pt (vs. 7pt) matches the default sub/super placement, but the slashes in the superscript tend to interfere, so I increased it.

The alignment of the stack is centered, though an optional argument of [l] or [r] on the \tabbedVectorStack can change that.

\documentclass{article}
\usepackage{tabstackengine}
\begin{document}
\setstackgap{L}{7pt}% SUB- SUPER- BASELINE SHIFT
\( L_1^2 \textrm{~versus~} L\raisebox{0.5pt}% SUB- BASELINE
  {\scriptsize\tabbedVectorstack{2,&3/4,&5/6\\1,&2,&3}} \)
\end{document}

enter image description here

6

enter image description here

Code

\[
L^{2,3/4,5/6}_{1, \hspace{9pt}2, \hspace{8pt}3}
\]

Second Version: where phantom is used instead. Thanks for the comments, leading to improvement.

enter image description here

Code:

\[
L^{2,3/4,5/6}_{1,\phantom{3/}2,\phantom{5/}3}  % This (shown here) aligns to the right.
\]

\[
L^{2,3/4,5/6}_{1,2\phantom{/4},3\phantom{/6}}  % This (not shown here) aligns to the left.
\]

\[
L^{2,3/4,5/6}_{1,\phantom{3}2\phantom{4},\phantom{5}3\phantom{6}} % This (also not shown here) aligns to the center.
\]

Conclusion: Pair up with superscripts and use phantom appropriately.

Jesse
  • 29,686
5

This is very simple and it almost works:

Almost: $ L^{2,}_{1,} {}^{3/4,}_{2,} {}^{5/6}_3 $

aligned sub/superscripts

However, the first superscript (2,) is smaller than the others, hence slightly lower. So here's how to fix it without guesswork about the dimensions (now simplifed thanks to @sgmoye):

After: $ L^{2,\mathstrut}_{1,} {}^{3/4,}_{2,} {}^{5/6}_3 $

enter image description here

Note that in other examples, some exponents might be too big to fit in the space TeX reserves with a \mathstrut; you'd then need to reserve vertical space in a more general way. Here's a (somewhat clumsy) way to do that:

Version 3: $ L^{2,\llap{\phantom{\scriptsize /}}}_{1,} {}^{3/4,}_{2,} {}^{5/6}_3 $

Here, our model for vertical space is a (scriptsize) slash; but it could be anything. \phantom makes it invisible, and \llap ensures that it claims no horizontal space.

alexis
  • 7,961
  • Even simpler, How about $ L{}^{2,\mathstrut}_{1,} {}^{3/4,}_{2,} {} ? – sgmoye Nov 01 '13 at 11:54
  • Yes! That's what I was looking for; tried regular \strut (which wasn't right at all), but didn't think to try \mathstrut for some reason. – alexis Nov 01 '13 at 12:33
0
L_{1,\phantom{3}2\phantom{4},\phantom{5}3\phantom{6}}^{2,3/4,5/6}

enter image description here

ADDENDUM: The \phantom macro puts a blank space. The space size is equal to the horizontal size of the enclosed argument. For example, \phantom{3} puts a blank space with a length which is equal to the horizontal size of 3, etc... So, it mimics the effect of something in there that we can not see it ( that is why it's call "phantom" ).

  • 1
    Please do not only write a line of code, explain what it is doing. Suppose the reader do not know what \phantom does ... A good answer contains an explanation ... – Mensch Nov 02 '13 at 09:24
  • Isn't this basically the same as @Jesse answered in the second part of his answer? – cgnieder Nov 02 '13 at 19:46
  • @cgnieder Yes. It's true. I use \phantom{...} a lot. I like pretty short answers whenever it's possible. Thanks. – Felix Marin Nov 02 '13 at 19:49