2

I have a trapezium node in TikZ that I would like to maintain consistent dimensions no matter how many times it is called in the \tikzpicture.

\documentclass[10pt]{article}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric}

\tikzstyle{object} = [draw, trapezium, trapezium left angle=70, trapezium right angle=-70, minimum width=3cm, minimum height=1cm, text centered, draw=black]

\begin{document}

\begin{tikzpicture}[node distance=2cm] \node(n1)[object]{a}; \node(n2)[object, right of=n1, xshift=1cm]{ab}; \node(n3)[object, right of=n2, xshift=1cm]{abc}; \end{tikzpicture}

\end{document}

Setting the minimum height and width seems to be the go-to for a lot of questions related to this topic but it has unexpected results on my end.

minimum width and height

The trapeziums are of different dimensions from each other and seem to grow in height the less character it has inside of them. Why does the node keep getting taller with fewer characters? What is it trying to accommodate? I would expect to get taller to house more texts by trying to keep the width constant.

% Same as before
\tikzstyle{object} = [draw, trapezium, trapezium left angle=70, trapezium right angle=-70, text width=3cm, text height=1cm, text centered, draw=black]
% Same as before

Using text height and text width instead of the minimum variants has the text off-center even after using text centered. Using both of them simultaneously results in the same image.

text width and height

I have also tried setting inner sep to various values but that does not work as well. Any solution is appreciated.

Rashiq
  • 337

1 Answers1

3

Setting text width and text height should cause TikZ to use a \parbox, but evidently does not use [c].

\documentclass[10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric}

\tikzset{object/.style={draw, trapezium, trapezium left angle=70, trapezium right angle=-70, draw=black, inner sep=0pt}}

\begin{document}

\begin{tikzpicture}[node distance=2cm] \node(n1)[object]{\parbox[c][1cm][c]{3cm}{\centering a}}; \node(n2)[object, right=1cm] at (n1.east) {\parbox[c][1cm][c]{3cm}{\centering ab}}; \node(n3)[object, right=1cm] at (n2.east) {\parbox[c][1cm][c]{3cm}{\centering abc}}; \node[draw=red,minimum width=3cm, minimum height=1cm] at (n1.center){}; \node[draw=red,minimum width=3cm, minimum height=1cm] at (n2.center){}; \node[draw=red,minimum width=3cm, minimum height=1cm] at (n3.center){}; \end{tikzpicture}

\end{document}

demo

John Kormylo
  • 79,712
  • 3
  • 50
  • 120
  • Thank you for the answer. Is there any way to do this with \tikzstyle and without specifying the \parbox... for every time the node is invoked? Wouldn't stating the \parbox every time decrease the maintainability of the document? – Rashiq Apr 26 '21 at 22:27
  • Aside from \tikzstyle being obsolete, there doesn't seem to be keyword for centering vertically. Note that text height=text depth doesn't quite center either due to the difference between \ht\strutbox and \dp\strutbox. You could simplify by creating a new command for the \parbox. – John Kormylo Apr 27 '21 at 04:01
  • On further reflection, text width probably uses \parbox, but text height and text depth probably use \raisebox. – John Kormylo Apr 27 '21 at 04:10