Consider the following diagram:
\begin{tikzcd}
\text{This is a sentence} \arrow[r, "\phi"] & x
\end{tikzcd}
I wish for the "This is" to be above "a sentence". I've tried using \\
to signal a new line, yet that merely causes an error.
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 communityYou want to use tabular
. If it's a one-off, then
\begin{tikzcd}
\begin{tabular}{@{}c@{}}This is \\ a sentence\end{tabular} \arrow[r, "\phi"] & x
\end{tikzcd}
is what you need. For several of these you may want a personal command, for instance
\newcommand{\splitentry}[2][c]{%
\begin{tabular}[#1]{@{}c@{}}#2\end{tabular}%
}
and your code would become
\begin{tikzcd}
\splitentry{This is \\ a sentence} \arrow[r, "\phi"] & x
\end{tikzcd}
I added an optional argument for the vertical alignment (default c
). Try with
\splitentry[t]{This is \\ a sentence}
\splitentry[b]{This is \\ a sentence}
to see the difference.
You may also use \makecell
:
\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz-cd}
\usepackage{makecell}
\begin{document}
\[
\begin{tikzcd}
\text{\makecell{This is\\a sentence}} \arrow[r, "\phi"] & x
\end{tikzcd}
\]
\end{document}
Of course, you can create a command:
\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz-cd}
\usepackage{makecell}
\newcommand{\mycell}[1]{\text{\makecell{#1}}}
\begin{document}
\[
\begin{tikzcd}
\mycell{This is\\ a sentence} \arrow[r, "\phi"] & x
\end{tikzcd}
\]
\end{document}
In both cases the result is: