如何在 TikZ 节点文本中将整个表格环境左对齐或右对齐?

如何在 TikZ 节点文本中将整个表格环境左对齐或右对齐?

我在矩阵中有一些节点代表基本粒子。一些节点代表粒子,这些节点下面的节点代表粒子的特征(例如电荷)。在具有特征的节点处有表格环境,用于对齐特征的名称和编号。当我希望这些表格环境左对齐时,它们会在节点中居中。

比较伽马方块的文本和 Z 方块的文本,可以看出特征文本定位的差异。如何更改代码,使所有方块的表格环境都以类似的方式对齐(即“质量”等的左边缘与方块边缘保持恒定距离)?

\documentclass[a4paper]{article}

\usepackage[english]{babel}
\usepackage[T1]{fontenc}
\usepackage[ansinew]{inputenc}
\usepackage{lmodern}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{amsfonts}
\usepackage{tikz}
\usepackage{verbatim}
\usepackage[active,tightpage]{preview}
\usepackage{siunitx}
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{5pt}
\usetikzlibrary{fit}
\usetikzlibrary{backgrounds}

\begin{document}

\tikzstyle{particleblue}=[
    rectangle,
    thick,
    draw=black,
    fill=blue!20,
    minimum size=80pt
]
\tikzstyle{particlegreen}=[
    rectangle,
    thick,
    draw=black,
    fill=green!20,
    minimum size=80pt
]
\tikzstyle{particlered}=[
    rectangle,
    thick,
    draw=black,
    fill=red!20,
    minimum size=80pt
]
\tikzstyle{particleyellow}=[
    rectangle,
    thick,
    draw=black,
    fill=yellow!40,
    minimum size=80pt
]

\tikzstyle{backgroundblue}=[
    rectangle,
    fill=blue!10,
    inner sep=0.2cm
]
\tikzstyle{backgroundgreen}=[
    rectangle,
    fill=green!10,
    inner sep=0.2cm
]
\tikzstyle{backgroundred}=[
    rectangle,
    fill=red!10,
    inner sep=0.2cm
]
\tikzstyle{backgroundyellow}=[
    rectangle,
    fill=yellow!40,
    inner sep=0.2cm
]

\newcommand\particleblue[5]{% {label}{name}{mass}{charge}{spin}
    \node (#1) [particleblue]{
        \Huge${\boldsymbol{#2}}$};
    \node[below=11pt]{%
        \tiny
        \begin{tabular}{ll}
        Mass:&${#3}$\\
        Charge:&${#4}$\\
        Spin:&${#5}$
        \end{tabular}
    };
}
\newcommand\particlegreen[5]{% {label}{name}{mass}{charge}{spin}
    \node (#1) [particlegreen]{
        \Huge${\boldsymbol{#2}}$};
    \node[below=11pt]{%
        \tiny
        \begin{tabular}{ll}
        Mass:&${#3}$\\
        Charge:&${#4}$\\
        Spin:&${#5}$
        \end{tabular}
    };
}
\newcommand\particlered[5]{% {label}{name}{mass}{charge}{spin}
    \node (#1) [particlered]{
        \Huge${\boldsymbol{#2}}$};
    \node[below=11pt]{%
        \tiny
        \begin{tabular}{ll}
        Mass:&${#3}$\\
        Charge:&${#4}$\\
        Spin:&${#5}$
        \end{tabular}
    };
}
\newcommand\particleyellow[5]{% {label}{name}{mass}{charge}{spin}
    \node (#1) [particleyellow]{
        \Huge${\boldsymbol{#2}}$};
    \node[below=11pt]{%
        \tiny
        \begin{tabular}{ll}
        Mass:&${#3}$\\
        Charge:&${#4}$\\
        Spin:&${#5}$
        \end{tabular}
    };
}

\begin{tikzpicture}[>=latex,text depth=0.00ex]
    % elements in a matrix
    \matrix[row sep=0.5cm,column sep=0.5cm, nodes={align=left}]{
        \particleblue{u}{u}{2.3\,\si{MeV/c^2}}{2/3}{1/2}&
        \particleblue{c}{c}{1.275\,\si{GeV/c^2}}{2/3}{1/2}&
        \particleblue{t}{t}{173.07\,\si{GeV/c^2}}{2/3}{1/2}&
        \particlered{g}{g}{0}{0}{1}&
        \particlegreen{H}{H}{126\,\si{GeV/c^2}}{0}{0}&
        \\
        \particleblue{d}{d}{4.8\,\si{MeV/c^2}}{-1/3}{1/2}&
        \particleblue{s}{s}{95\,\si{MeV/c^2}}{-1/3}{1/2}&
        \particleblue{b}{b}{4.18\,\si{GeV/c^2}}{-1/3}{1/2}&
        \particlered{gamma}{\gamma}{0}{0}{1}&
        \\
        \particleyellow{e}{e}{0.511\,\si{MeV/c^2}}{-1}{1/2}&
        \particleyellow{mu}{\mu}{105.7\,\si{MeV/c^2}}{-1}{1/2}&
        \particleyellow{tau}{\tau}{1.777\,\si{GeV/c^2}}{-1}{1/2}&
        \particlered{Z}{Z}{91.2\,\si{GeV/c^2}}{0}{1}&
        \\
        \particleyellow{nu_e}{\nu_{e}}{<2.2\,\si{eV/c^2}}{0}{1/2}&
        \particleyellow{nu_mu}{\nu_{\mu}}{0.17\,\si{MeV/c^2}}{0}{1/2}&
        \particleyellow{nu_tau}{\nu_{\tau}}{15.5\,\si{MeV/c^2}}{0}{1/2}&
        \particlered{W}{W}{80.4\,\si{GeV/c^2}}{\pm{1}}{1}&
        \\
    };
    % background rectanges
    \begin{pgfonlayer}{background}
        \node [
            backgroundblue,
            fit=(u) (b)
        ]{};
        \node [
            backgroundyellow,
            fit=(e) (nu_tau)
        ]{};
        \node [
            backgroundred,
            fit=(g) (W)
        ]{};
        \node [
            backgroundgreen,
            fit=(H)
        ]{};
    \end{pgfonlayer}
\end{tikzpicture}
\end{document}

答案1

一种完全不同的图像设计方法:

\documentclass[a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{backgrounds,fit,positioning}
\usepackage[active,tightpage]{preview}
\usepackage{amsmath}
\usepackage{siunitx}
\usepackage{tabularx}
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{5pt}
    \newcommand\ppbb{path picture bounding box}

\begin{document}

\begin{tikzpicture}[
    node distance = 4mm and 6mm,
particle/.style args = {#1/#2/#3/#4/#5}{
        rectangle, draw, thick, fill=#2, 
        text width=80pt, minimum height=80pt,
        font=\Large\itshape\bfseries, align=center,
        node contents={#1\\ \mbox{}},
        path picture={
        \node [%
            inner sep=4pt, outer sep=0pt,
            text width=80pt, minimum height=20pt, align=center,
            font=\scriptsize,
            above=0pt of \ppbb.south]
            {\begin{tabularx}{\hsize}{@{}Xl@{}}
            Mass:   &   #3      \\
            Charge: &   $#4$    \\
            Spin:   &   $#5$
             \end{tabularx}
            };%end of node contents
                      }% end of path picture 
                         }% end particle style               
                ]
\node (p12) [particle=d/blue!20/\SI{2.3}{MeV/c^2}/{2/3}/{1/2}];
\node (p22) [particle=s/blue!20/\SI{95}{MeV/c^2}/{2/3}/{1/2},
             right=of p12];
\node (p32) [particle=b/blue!20/\SI{4.18}{MeV/c^2}/{-1/3}/{1/2},
             right=of p22] {};
\node (p42) [particle=$\boldsymbol{\gamma}$/red!20/ / / ,
             right=of p32];
%
\begin{pgfonlayer}{background}
    \node [fill=blue!10, fit=(p12) (p32)]{};
    \node [fill= red!10, fit=(p42)]{};
\end{pgfonlayer}

\end{tikzpicture}

\end{document}

通过使用 可以解决您的问题tabularx。正如您所注意到的,节点的设计完全不同。此节点可以轻松集成到您的\newcommand定义中,然后像您在解决方案中所做的那样在矩阵中使用。否则,可以按照上述 MWE 中提出的方式放置节点或将其连接成链。

在 MWE 中,我只考虑图像的第二行。

在此处输入图片描述

答案2

@{}p{17pt}p{43pt}@{}尝试使用粒子的表格环境规范,例如:

\newcommand\particleblue[5]{% {label}{name}{mass}{charge}{spin}
    \node (#1) [particleblue]{
        \Huge${\boldsymbol{#2}}$};
    \node[below=11pt]{%
        \tiny
        \begin{tabular}{@{}p{17pt}p{43pt}@{}}
        Mass:&${#3}$\\
        Charge:&${#4}$\\
        Spin:&${#5}$
        \end{tabular}
    };
}

在此处输入图片描述

相关内容