如何将此 TikZ 图片缩放到图中的文本宽度?

如何将此 TikZ 图片缩放到图中的文本宽度?

我有一张包含节点、矩阵和一些其他元素的 TikZ 图片。我有点不知道如何缩放它以适合图中的文本宽度。使用会resizebox导致奇怪的渲染错误,并且 TikZ 图片scale参数似乎没有任何作用。我该怎么做?

\documentclass[american, a4paper]{report}

\usepackage{graphicx}
\usepackage{pgf}
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{calc}
\usetikzlibrary{arrows, shapes}
\usetikzlibrary{arrows, automata}
\usetikzlibrary{positioning}
\usepackage{subfigure}
\usepackage{float}

%\usepackage{gantt}
\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground}

\usepackage{pdflscape}
\usepackage{amsmath}
\usepackage{gensymb}
\usepackage{bm}
\usepackage{cancel}
\usepackage{mathtools}
\usetikzlibrary{fit, arrows, calc, positioning}
\DeclarePairedDelimiter{\ceil}{\lceil}{\rceil}
\DeclarePairedDelimiter{\floor}{\lfloor}{\rfloor}
\usepackage{feynmp}
\usepackage{feynmp-auto}
\unitlength=1.00 mm
\usepackage{bbding}
\usepackage[hidelinks]{hyperref}
\usepackage{verbatim}
\usepackage{fancyvrb}
\usepackage{ragged2e}
\usepackage{lipsum}
\usepackage{multicol}
\usepackage{multirow}
\usepackage{babel}
\usepackage{datetime}
\usepackage{siunitx}
\usepackage{lineno}
\usepackage{attachfile}
\usepackage{fancyhdr}
\usepackage{titlesec}
\newcommand{\mathunit}[2]{#1\,\si{#2}}
\usepackage{parskip}

\begin{document}

\begin{figure}[H]

\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}{@{}p{17pt}p{43pt}@{}}
        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}{@{}p{17pt}p{43pt}@{}}
        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}{@{}p{17pt}p{43pt}@{}}
        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}{@{}p{17pt}p{43pt}@{}}
        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}

\caption{Standard Model fundamental particles in their mass eigenstates, with matter generations distinguished by column}
\label{figure:Standard_Model_particles}
\end{figure}

\end{document}

答案1

你还有更多的可能性:

  • 考虑epR8GaYuh评论(因此质量、电荷和自旋的数据将变得非常小,以至于需要放大镜才能阅读)
  • 全局更改页面布局,例如添加\usepackage{geometry}序言
  • 使用包pagechange及其宏等方法局部改变图形环境\adjustwidth<+{}{-\dimexpr\marginparwidth+\marginparsep\relax}
  • 重新设计你的身材(让所有元素变得更小

第二种和第四种可能性的组合给出:

在此处输入图片描述

MWE(删除所有不相关的包和 tikz 库并添加缺失的包和替换贬值的包并调整图像元素/样式后\tikzstyle\tikzset是:

\documentclass{report}
\usepackage{geometry}
\usepackage{showframe}
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{calc}
\usetikzlibrary{arrows, fit, positioning}
\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground}

\usepackage{amsmath} 
\usepackage{siunitx}

\begin{document}
    \begin{figure}
\setlength\tabcolsep{4pt}
\tikzset{
          base/.style = {shape=rectangle,
                         draw, thick,
                         minimum size=75pt,
                         inner sep=2pt,
                         outer sep=0pt},
  particleblue/.style = {base, fill=blue!20},
 particlegreen/.style = {base, fill=green!20},
   particlered/.style = {base, fill=red!20},
particleyellow/.style = {base, fill=yellow!40},
%
         basefit/.style = {shape=rectangle,
                           inner sep=2pt
                           outer sep=0pt},
  backgroundblue/.style = {fill=blue!10},
 backgroundgreen/.style = {fill=green!10},
   backgroundred/.style = {fill=red!10},
backgroundyellow/.style = {fill=yellow!40}
        } % end of tikzset

\newcommand\particleblue[5]{% {label}{name}{mass}{charge}{spin}
    \node (#1) [particleblue]{
        \Huge${\boldsymbol{#2}}$};
    \node[below=11pt]{%
        \tiny
        \begin{tabular}{@{}p{17pt}p{42pt}@{}}
        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}{@{}p{17pt}p{42pt}@{}}
        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}{@{}p{17pt}p{42pt}@{}}
        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}{@{}p{17pt}p{42pt}@{}}
        Mass:  &${#3}$\\
        Charge:&${#4}$\\
        Spin:  &${#5}$
        \end{tabular}
    };
}
\begin{tikzpicture}[>=latex]
    % elements in a matrix
    \matrix[row sep=9pt,column sep=8pt, 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}

\caption{Standard Model fundamental particles in their mass eigenstates, with matter generations distinguished by column}
\label{figure:Standard_Model_particles}
    \end{figure} 
\end{document}

如果 代替MassCharge并且Spin使用 短度MC并且S以及其含义在标题中解释,则可以进一步减小图像大小。这可以进一步减少节点。

答案2

如果你尝试将其tikzpicture放入任何盒子中,就会发生奇怪的事情。所以我改用lrbox环境。

\documentclass[american, a4paper]{report}

\usepackage{graphicx}
\usepackage{pgf}
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{calc}
\usetikzlibrary{arrows, shapes}
\usetikzlibrary{arrows, automata}
\usetikzlibrary{positioning}
\usepackage{subfigure}
\usepackage{float}

%\usepackage{gantt}
\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground}

\usepackage{pdflscape}
\usepackage{amsmath}
\usepackage{gensymb}
\usepackage{bm}
\usepackage{cancel}
\usepackage{mathtools}
\usetikzlibrary{fit, arrows, calc, positioning}
\DeclarePairedDelimiter{\ceil}{\lceil}{\rceil}
\DeclarePairedDelimiter{\floor}{\lfloor}{\rfloor}
\usepackage{feynmp}
\usepackage{feynmp-auto}
\unitlength=1.00 mm
\usepackage{bbding}
\usepackage[hidelinks]{hyperref}
\usepackage{verbatim}
\usepackage{fancyvrb}
\usepackage{ragged2e}
\usepackage{lipsum}
\usepackage{multicol}
\usepackage{multirow}
\usepackage{babel}
\usepackage{datetime}
\usepackage{siunitx}
\usepackage{lineno}
\usepackage{attachfile}
\usepackage{fancyhdr}
\usepackage{titlesec}
\newcommand{\mathunit}[2]{#1\,\si{#2}}
\usepackage{parskip}

\newsavebox{\tempbox}

\begin{document}

\begin{figure}[H]

\begin{lrbox}{\tempbox}
\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}{@{}p{17pt}p{43pt}@{}}
        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}{@{}p{17pt}p{43pt}@{}}
        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}{@{}p{17pt}p{43pt}@{}}
        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}{@{}p{17pt}p{43pt}@{}}
        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}% NOTE: remove last &
        \\
        \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}
%\draw[red] (current bounding box.south west) rectangle (current bounding box.north east);
\end{tikzpicture}
\end{lrbox}
\resizebox{\textwidth}{!}{\usebox{\tempbox}}

\caption{Standard Model fundamental particles in their mass eigenstates, with matter generations distinguished by column}
\label{figure:Standard_Model_particles}
\end{figure}

\end{document}

演示

相关内容