0

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.

0

2 Answers 2

2

You 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.

0

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:

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .