如何在 tikz 矩陣的節點中定義文字

如何在 tikz 矩陣的節點中定義文字

我有以下程式碼。

我想透過簡單地在 & 符號之間添加文字來將文字添加到我的節點。執行此操作的語法是什麼?

例如,我嘗試在第二列的第一行添加文字“John Doe”。文字必須在 John 和 Doe 之間包含換行符。我通常使用的語法是這樣的:

\node[draw, align = center]{John \\ Doe};

在我在矩陣選項中預定義節點的矩陣中,如何執行此操作?

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}


\begin{tikzpicture}
\matrix(a)[matrix of nodes , nodes in empty cells , nodes = {draw , circle , align = center} ,
column sep={1cm,between origins} , row sep = {1cm , between origins}]
{
& John \\ Doe    &      & \\
&        &     &\\
};

\draw[->] (a-1-1) -- (a-2-1);

\end{tikzpicture}
\end{document}

答案1

如果你給你的節點一個固定的,你可以加入換行符。例如:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}

\begin{tikzpicture}
\matrix(a)[
  matrix of nodes, 
  nodes in empty cells, 
  nodes={draw, circle, align=center, text width=0.8cm},
  column sep={1.5cm, between origins}, 
  row sep={1.5cm, between origins}
]{
  & John \linebreak Doe & & \\
  & & &\\
};

\draw[->] (a-1-1) -- (a-2-1);

\end{tikzpicture}
\end{document}

在此輸入影像描述

相關內容