1

I have some TikZ code of which I would like to widen the bottom part of the output while preserving the width of the top. (Bad formulation, don't know how to formulate this well.)

For example, say I have some square like

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}

% Not my actual code snippet, but for simplicity, let's just draw a square \draw (-1, -1) -- (1, -1) -- (1, 1) -- (-1, 1) -- cycle;

\end{tikzpicture} \end{document}

Now, for this simple example, it is easy to widen the base of the output by simply drawing an isosceles trapezoid instead of a square like

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}

% The top's width is preserved, while the bottom's width is stretched \draw (-2, -1) -- (2, -1) -- (1, 1) -- (-1, 1) -- cycle;

\end{tikzpicture} \end{document}

However, for more complex code snippets, this not accessible for me.

How can I transform a generic rectangular-like shape input into a isosceles trapezoid-like shape output? Could the scope environment be of use?

Stephen
  • 3,826
aahlback
  • 45
  • 5
  • I don't understand the problem, if you need a trapezium just draw one (or use the shape trapezium if applicable). What do you mean with “more complex code snippets”? What's not accessible to you? As I see it a transformation that transform a rectangle into a trapezium is non-linear and that's no fun in TikZ. – Qrrbrbirlbel Sep 05 '23 at 11:42
  • By the way, your second example is a triangle (which can be considered a trapezium, sure). – Qrrbrbirlbel Sep 05 '23 at 11:44
  • 1
  • @Qrrbrbirlbel Sorry for not being clear enough, I don't know how to properly formulate the problem properly. Just to give another simple example in the absence of a better formulation: Say I have a square image (in TikZ) that I want to transform to a isosceles trapezoid image without any cropping occuring. – aahlback Sep 05 '23 at 12:08
  • 1
    @dexteritas Thanks! It would be nice to avoid using external TikZ-packages, but I suppose that tikz3d.dtx will solve the problem. I'll give it a try. – aahlback Sep 05 '23 at 12:15

1 Answers1

1

Here's a way to draw and re-use isosceles trapezoids using \pic statements:

  • you define what to draw inside a \pic-style
  • you use \pic very similar to \node.

The solution comes in two parts:

    1. drawing a fixed trapezoid, to show basics
    1. allowing parameters passed to a \pic, a bit advanced

result

Part 1: fixed trapezoid

The basic idea is simple:

  • define 4 coordinates
  • draw a closed line (that's what cycle does)
  • call this pic-style e.g. trap

Call \pic as shown, and set some options.

\pic at (0,0) {trap};
Part 2: flexible trapezoid

What's new here is passing 3 arguments, where I decided to use / as delimiters. The overhead for this trap is a bit different, but you should spot the same coordinates and drawing as above. Also note the replacement by the arguments #1, #2 and #3. Specifying pic and arguments:

\pic at (0,0) {trap={2/4/1}};
Code:
\documentclass[10pt,border=3mm,tikz]{standalone}

\begin{document} \begin{tikzpicture}[ trap/.pic={ \coordinate (A) at ( 3,0); \coordinate (B) at (-3,0); \coordinate (C) at (-2,1); \coordinate (D) at ( 2,1); %
\draw (A) -- (B) -- (C) -- (D) -- cycle; } ] \pic at (0,0) {trap}; \pic[scale=.5,rotate=30] at (0,-2) {trap}; \end{tikzpicture}

\begin{tikzpicture} [ pics/trap/.style args={#1/#2/#3}% bottom, top, height { code={ \coordinate (A) at ( #1,0); \coordinate (B) at (-#1,0); \coordinate (C) at (-#2,#3); \coordinate (D) at ( #2,#3); %
\draw (A) -- (B) -- (C) -- (D) -- cycle; } } ] \pic at (0,0) {trap={2/4/1}}; \pic[scale=.5,rotate=-30] at (0,-2) {trap={3/1/2}}; \end{tikzpicture} \end{document}

MS-SPO
  • 11,519