4

The shortcomings of tikz trapezium shape have already been highlighted. I am looking for a parallelogram shape that strictly respects given angles and text width, while allowing for vertical stretching to fit the node text (I don't mind the parallelogram's width increasing to accomodate these constraints). This could be done by drawing the parallelogram using postaction on a rectangular shape if it is possible to reference the anchors of the current node in said postaction. Hence the question : how to access current node's anchors in a postaction ?

EDIT : the following code shows how parallelograms are distorted (incorrect length without stretch, incorrect angles with stretch). It also presents the approach I mentioned earlier : style parallelogram has to be passed an existing node as argument, and draws a parallelogram around that node. I would like to be able to pass it the node it is applied to, in postaction (and as I'm writing this I'm wondering if postaction is needed at all).

\documentclass{minimal}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{shapes}
\usetikzlibrary{calc}

% solution using trapezium
\tikzstyle{para} = [trapezium, trapezium left angle=60,
    trapezium right angle=120, draw=black, text width = 3cm,
    minimum height=.8cm]

% attempt at custom solution
\tikzset{parallelogram/.code={
    \draw[red] (#1.north west)
        -- ($(#1.south west)!.2!90:(#1.north west)$)
        -- (#1.south east)
        -- ($(#1.north east)!.2!90:(#1.south east)$)
        -- cycle;
}}

\begin{document}

Para(llelogram) shape using trapezium (no stretch) :

\begin{tikzpicture}
\node[para] {with a longer text with a longer text with a longer text with a longer text};
\node[para] at (0,2) {short};
\node[para] at (0,3) {.};
\end{tikzpicture}

Para(llelogram) shape using trapezium (trapezium stretches):

\begin{tikzpicture}[trapezium stretches]
\node[para] {with a longer text with a longer text with a longer text with a longer text};
\node[para] at (0,2) {short};
\node[para] at (0,3) {.};
\end{tikzpicture}

Para(llelogram) shape using trapezium (trapezium stretches body):

\begin{tikzpicture}[trapezium stretches body]
\node[para] {with a longer text with a longer text with a longer text with a longer text};
\node[para] at (0,2) {short};
\node[para] at (0,3) {.};
\end{tikzpicture}

Custom solution proposal:

\begin{tikzpicture}
\node[rectangle, densely dotted, draw, text width=3cm] (a) at (2,1) {test};
\node[rectangle, parallelogram=a] at (6,1) {dummy};
\node[rectangle, densely dotted, draw, text width=3cm] (b) at (2,-1) {with a longer text with a longer text with a longer text with a longer text};
\node[rectangle, parallelogram=b] at (6,-1) {dummy2};
\end{tikzpicture}
\end{document}

EDIT 2: result of proposed custom solution enter image description here

  • 1
    can you show us an example ? Also did you try the keys trapezium stretches ... both equal to false? – percusse Jun 15 '16 at 15:35
  • I tried all the stretch options, with the consequences that were noted in the message I linked earlier. I'm updating my message to clarify my intent (sorry should have done it earlier). – Christoph Frings Jun 15 '16 at 18:12
  • Please don't use minimal for examples. It is not designed for this and can be very misleading in certain cases. – cfr Jun 15 '16 at 21:32
  • Noted, thx, I remembered that one of minimal or standalone had to be avoided and picked the wrong one. – Christoph Frings Jun 16 '16 at 6:59
2

You can use append after command (or similar) to add a path to the node, referencing the node using \tikzlastnode.

For example and using a trick I got from Alenanno:

\documentclass[tikz,border=10pt,multi]{standalone}
\usetikzlibrary{calc}
\tikzset{
  parallelogram/.style={
    append after command={% courtesy of Alenanno ref: https://tex.stackexchange.com/questions/287967/drawing-thin-line-around-a-multipart-tikz-shape#comment696552_287972
      \pgfextra{\draw[red] (\tikzlastnode.north west) -- ($(\tikzlastnode.south west)!.2!90:(\tikzlastnode.north west)$) -- (\tikzlastnode.south east) -- ($(\tikzlastnode.north east)!.2!90:(\tikzlastnode.south east)$) -- cycle;}
    }
  }
}

\begin{document}
\begin{tikzpicture}
  \node [rectangle, densely dotted, draw, text width=3cm, parallelogram] (a) at (2,1) {test};
  \node [rectangle, parallelogram] at (6,1) {dummy};
  \node [rectangle, densely dotted, draw, text width=3cm, parallelogram] (b) at (2,-1) {with a longer text with a longer text with a longer text with a longer text};
  \node [rectangle, parallelogram] at (6,-1) {dummy2};
\end{tikzpicture}
\end{document}

produces

parallelograms around fixed-width nodes

| improve this answer | |
  • Excellent, that's what I needed. Thx ! – Christoph Frings Jun 16 '16 at 7:00

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.