如何获得指定尺寸的几何形状?

如何获得指定尺寸的几何形状?

我希望在 TikZ 中获得几何形状,其大小与其中可能包含的任何文本无关。考虑这个 MWE:

\documentclass[tikz]{standalone}
\usetikzlibrary{shapes.geometric}
\tikzset{trap/.style={shape=trapezium,draw=black,minimum width=1.0cm,
  minimum height=1.0cm,shape border uses incircle}}
\begin{document}
\begin{tikzpicture}
\tikzset{trapezium stretches=true}
\node[trap=green] (trap) at (-1,0) {};
\node[trap=green] (trap) at (1,0) {A};
\end{tikzpicture}
\end{document}

它产生以下内容:

在此处输入图片描述

(顶部被切掉与 TikZ 无关,而是与我导出时 Apple 的预览有关......)

如何才能使尺寸与形状中的任何文本无关?

答案1

获得独立于文本的大小的一种方法是将文本作为标签放置。为了方便起见,我稍微重新定义了一下,trap以便第一个参数真正被使用,而第二个参数是上述标签。不用说,如果文本太宽,它就会超出范围。(而且预览不会在我的 Mac 上剪切图片。)

\documentclass[tikz]{standalone}
\usetikzlibrary{shapes.geometric}
\tikzset{trap/.style 2 args={shape=trapezium,draw=black,minimum width=1.0cm,draw=#1,label=center:#2,
  minimum height=1.0cm,shape border uses incircle,}} % text width=0pt,text height=1em,align=flush center
\begin{document}
\begin{tikzpicture}
\tikzset{trapezium stretches=true}
\node[trap={green}{}] (trap) at (-1,0) {};
\node[trap={green}{A}] (trap) at (1,0) {};
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

您还可以使用text width=width("<put here your larger text>")具有较大文本宽度的所有节点。

我也在您的样式定义#1之后添加了该参数。draw=

\documentclass[tikz]{standalone}
\usetikzlibrary{shapes.geometric}
\tikzset{trap/.style={shape=trapezium,draw=#1,minimum width=1.0cm,
  minimum height=1.0cm,shape border uses incircle,
  text width=width("A"),%put here the larger text
  text centered}}
\begin{document}
\begin{tikzpicture}
\tikzset{trapezium stretches=true}
\node[trap=green] (trap) at (-1,0) {};
\node[trap=green] (trap1) at (1,0) {A};
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容