Using tikz to draw arrows in text

tikz-arrowstikz-pgftikz-styles

I am trying to do the following:
enter image description here

where I want to draw the up and down arrows within text. I first assumed it to be tikz, so below is my MWE. It is weirdly shifted up a little bit, so please help me recreate the above example of arrows.

\newcommand{\upspin}{\tikz[scale=0.5]{\draw[-Latex] (0,0) -- (0,0.6cm);}}
This is the spin-up (\upspin) electron and the spin-down (\dspin).

Please let me know.

Best Answer

With option \tikz[baseline=<length>] and relative length unit em:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}
\usepackage{lipsum}

\newcommand{\upspin}{%
  \tikz[baseline=.2em] \draw[-Latex] (0,0) -- (0,1em);}
\newcommand{\dspin}{%
  \tikz[baseline=.2em] \draw[Latex-] (0,0) -- (0,1em);}

\begin{document}
\lipsum*[1][1-3]
This is the spin-up (\upspin) electron and the spin-down (\dspin).
\lipsum*[1][1-3]
\end{document}

enter image description here

PS: You can search for [tikz] baseline on this site or in the pgfmanual to learn more about this option.

Update: Following hpekristiansen's suggestion, a revised version using ex unit. Note the used depth and height setting may not work for all fonts.

One could, of course measure the height and depth of ( in text mode and use them in \upspin and \dspin, and even insert the measuring code in latex2e hook selectfont (\AddToHook{selectfont}{...}) for efficiency concerns.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}
\usepackage{lipsum}

\newcommand{\upspin}{%
  % depth = .5ex, height = 1.75ex
  \smash{\tikz[baseline=0pt] \draw[-Latex] (0,-.5ex) -- (0,1.75ex);}}
\newcommand{\dspin}{%
  \smash{\tikz[baseline=0pt] \draw[Latex-] (0,-.5ex) -- (0,1.75ex);}}

\begin{document}
\lipsum*[1][1-3]
This is the spin-up (\upspin) electron and the spin-down (\dspin).
\lipsum*[1][1-3]
\end{document}

enter image description here

Related Question