[Tex/LaTex] tikz – trapezium node – fix angle and height, adjustable width

tikz-pgf

Using TikZ, I want to draw trapezium nodes all of which have a fixed height and angle(s), but whose widths vary. I can't find any combination of parameters that does what I want. Specifically, with trapezium stretches=false, the angle is correct but the widths are ridiculously large. With trapezium stretches=true or trapezium stretches body=true, the widths are correct but the angles are all wrong.

A similar problem seems to have been discussed at Drawing parallelogram with fixed angle, width and height?, but I don't see an answer in there, only talk about why the obvious thing doesn't work, which frankly I don't care, I'm only interested in what to change so that it does work. (One clarification, though: these trapezoidal bars need to be nodes so that they can be addressed in the larger document, for labeling and so on. Also, I'm setting the size of the node with \rule because when I tried to do it exclusively with minimum width I got division-by-zero errors. It appears that a trapezium node cannot be empty.)

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric}
\usepackage[active,pdftex,tightpage]{preview}
\PreviewEnvironment[]{tikzpicture}
\begin{document}
\begin{tikzpicture}[x=5mm,y=5mm,every node/.style={
  trapezium, trapezium angle=67.5, draw,
  inner sep=0pt, outer sep=0pt,
  minimum height=1.81mm, minimum width=0pt
}]
\node [] at (0,9) {\rule{1pt}{0.1pt}};
\node [] at (0,8) {\rule{5pt}{0.1pt}};
\node [] at (0,7) {\rule{10pt}{0.1pt}};
\node [trapezium stretches] at (0,6) {\rule{1pt}{0.1pt}};
\node [trapezium stretches] at (0,5) {\rule{5pt}{0.1pt}};
\node [trapezium stretches] at (0,4) {\rule{10pt}{0.1pt}};
\node [trapezium stretches body] at (0,3) {\rule{1pt}{0.1pt}};
\node [trapezium stretches body] at (0,2) {\rule{5pt}{0.1pt}};
\node [trapezium stretches body] at (0,1) {\rule{10pt}{0.1pt}};
\end{tikzpicture}
\end{document}

which renders as:

rendering of tikz code above, demonstrating problem with width/angle

Best Answer

One possibility would be to use text width to control the width; using one argument for a new mytrap style, you the can use the style like this:

\node [mytrap=<width>] at (<position>) {};

A complete example:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric}
%\usepackage[active,pdftex,tightpage]{preview}
%\PreviewEnvironment[]{tikzpicture}
\begin{document}
\begin{tikzpicture}[x=5mm,y=5mm,
  mytrap/.style={
  trapezium, trapezium angle=67.5, draw,inner xsep=0pt,outer sep=0pt,
  minimum height=1.81mm, text width=#1
}]
\foreach \ancho [count=\xi] in {5,10,...,100}  
  \node [mytrap=\ancho pt] at (0,-\xi) {};
\end{tikzpicture}
\end{document}

enter image description here