0

I want these two lines to align properly, \textcolor arrow adds extra space

\begin{align}
    \ket{\rm P}_t =& 
    \ket{\uparrow\textcolor{red}{\uparrow}\uparrow} \\ \nonumber &
    \ket{\uparrow\uparrow\uparrow}
\end{align}

result

3
  • Welcome! There is a new \mathcolor. I have not tried it, but perhaps it gives the expected results in this case?
    – mickep
    Sep 6, 2022 at 19:12
  • 1
    Welcome to TeX.SE. Please, when posting a question, always post a Minimal Working Example.
    – FHZ
    Sep 6, 2022 at 19:21
  • I'm voting to leave open, it would have been nice if the OP had provided an MWE but even without it the question was rather clear and answerable (as evidenced by the three answers). In general I'm in favor of closing for no MWE even if the question is easy to understand as a way of 'teaching' OP that MWEs are generally important in order to get answers, but if (good) answers are already provided then it seems rather pointless to close clear questions (the teachable moment has passed, so to speak).
    – Marijn
    Sep 7, 2022 at 13:14

3 Answers 3

3

Three Rel atoms behaves like three Ord atoms: there is no spaces between them. This is case of your second line. But the construction \textcolor{red}{\uparrow} creates Ord atom, so you have a triple Rel Ord Rel and TeX inserts spaces between them. But you can re-type Rel atoms to Ord by encapsulating them to {...}, so your desired behavior can be reached by:

\ket{{\uparrow}\textcolor{red}{\uparrow}{\uparrow}} \\ \nonumber &
1

In the “all black” case you're being lucky, because \uparrow is defined as a relation symbol and TeX adds

  • no space between an opening delimiter and a relation symbol;
  • no space between relation symbols;
  • no space between a relation symbol and a closing delimiter.

In the colored case, the middle arrow is treated as an ordinary symbol and TeX adds a thick space between a relation symbol and an ordinary one, in either order.

Solution. Define a command for an up arrow to be considered as an ordinary symbol: all it takes is to brace \uparrow. You can also add an optional argument for the color.

This requires an up-to-date TeX distribution:

\documentclass{article}
\usepackage{amsmath}
\usepackage{braket}
\usepackage{xcolor}

\NewDocumentCommand{\xuparrow}{o}{{%
  \IfNoValueTF{#1}{\uparrow}{\mathcolor{#1}{\uparrow}}%
}}

\begin{document}

\[
\ket{\mathrm{P}_t} = 
\ket{\xuparrow\xuparrow[red]\xuparrow}
\]

\end{document}

enter image description here

For older LaTeX versions you can resort to

\documentclass{article}
\usepackage{amsmath}
\usepackage{braket}
\usepackage{xcolor}

\newcommand{\xuparrow}[1][]{{%
  \if\relax\detokenize{#1}\relax
    \uparrow
  \else
    \textcolor{#1}{\uparrow}%
  \fi
}

\begin{document}

\[
\ket{\mathrm{P}_t} = 
\ket{\xuparrow\xuparrow[red]\xuparrow}
\]

\end{document}

Choose a more descriptive name for the command, based on its meaning.

Also note that \rm has been deprecated for almost 30 years.

0

A very simple solution would be to wrap the correspondent black arrow inside curly brackets. Arrows are properly aligned, but not necessarily the alignment you want. I removed \ket since I don't know from which package it comes from.

\documentclass{article}
\usepackage{amsmath,xcolor}
\begin{document}
\begin{align}
  a & = \uparrow\textcolor{red}{\uparrow}\uparrow \\
  b & = \uparrow{\uparrow}\uparrow
\end{align}
\end{document}

enter image description here

1
  • 1
    This adds space everywhere except after the = sign, where it is wanted. \bra and \ket are defined by the braket package, which is used in the OP's example. Sep 6, 2022 at 23:27

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.