TikZ 节点中的换行符

TikZ 节点中的换行符

我有一段代码可以输出一个小的家谱

    \documentclass{article}
    \usepackage{tikz}
    \usetikzlibrary{trees}
    \begin{document}
    \begin{tikzpicture}[
      man/.style={rectangle,draw,fill=blue!20},
      woman/.style={rectangle,draw,fill=red!20,rounded corners=.8ex},
      grandchild/.style={grow=down,xshift=1em,anchor=west,
        edge from parent path={(\tikzparentnode.south) |- (\tikzchildnode.west)}},
      first/.style={level distance=6ex},
      second/.style={level distance=12ex},
      third/.style={level distance=18ex},
      level 1/.style={sibling distance=5em}]
        % Parents
        \coordinate
          child[grow=left] {node[man,anchor=east]{Jim}}
          child[grow=right] {node[woman,anchor=west]{Jane}}
          child[grow=down,level distance=0ex]
        [edge from parent fork down]
        % Children and grandchildren
        child{node[man] {Alfred}}
        child{node[woman] {Berta}}
        child {node[man] {Charles}}
        child {node[woman]{Doris}};        
    \end{tikzpicture}
    \end{document}

我想要做的是在每个框中写两行文本(例如 Jim 和他的出生日期下方)。我该如何换行才能做到这一点?\\不起作用。

以下是输出的屏幕截图:

截屏

答案1

您可以尝试在内容中使用表格(两行)(表格环境);但是,我没有所需的包来检查它是否可行或是否不允许。例如

  \begin{tabular}{c}
     Jim \\
     DOB \\
  \end{tabular}

如果\\由于包以某种方式更改而无法工作,那么您可以尝试\cr,但我认为它无法工作,如果它可以工作,那就是不好的 LaTeX(太多的 TeXish)。

答案2

align=center如果要使用,请添加到样式中\\

在此处输入图片描述

代码:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{trees}
\begin{document}
\begin{tikzpicture}[
  man/.style={rectangle,draw,fill=blue!20, align=center},
  woman/.style={rectangle,draw,fill=red!20,rounded corners=.8ex, align=center},
  grandchild/.style={grow=down,xshift=1em,anchor=west,
    edge from parent path={(\tikzparentnode.south) |- (\tikzchildnode.west)}},
  first/.style={level distance=6ex},
  second/.style={level distance=12ex},
  third/.style={level distance=18ex},
  level 1/.style={sibling distance=5em}]
    % Parents
    \coordinate
      child[grow=left] {node[man,anchor=east]{Jim}}
      child[grow=right] {node[woman,anchor=west]{Jane}}
      child[grow=down,level distance=0ex]
    [edge from parent fork down]
    % Children and grandchildren
    child{node[man] {Alfred \\ 05-04-83}}
    child{node[woman] {Berta \\ 05-04-99}}
    child {node[man] {Charles \\ 05-04-77}}
    child {node[woman]{Doris \\ 05-04-80}};        
\end{tikzpicture}
\end{document}

相关内容