3

I would like to get geometric shapes in TikZ whose size is independent of any text I may have in them. Consider this 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}

It produces the following:

enter image description here

(The top being cut off has nothing to do with TikZ, but with Apple's Preview when I export ...)

How can I get the size to be independent of any text that may be in the shape?

3

One way of getting the size independent of the text is to place the text as a label. For your convenience I slightly redefined trap such that the first argument really gets used and the second argument is the above-mentioned label. Needless to say that if the text gets too wide, it will overshoot. (And preview does not cut the picture on my 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}

enter image description here

  • Thanks. Not sure what is going on with Preview... – user1362373 Jun 30 '19 at 5:02
1

You could use also text width=width("<put here your larger text>") to have all the nodes with the text width of the lager text.

I also put #1 after draw= in the definition of your style, as you're using that parameter.

\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}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.