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}
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It only takes a minute to sign up.
Sign up to join this communityThree 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 &
In the “all black” case you're being lucky, because \uparrow
is defined as a relation symbol and TeX adds
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}
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.
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}
=
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
\mathcolor
. I have not tried it, but perhaps it gives the expected results in this case?