tikz梯形的缺点已经被突出显示。我正在寻找一种平行四边形,它严格遵守给定的角度和文本宽度,同时允许垂直拉伸以适应节点文本(我不介意增加平行四边形的宽度以适应这些约束)。如果可以在所述后动作中引用当前节点的锚点,则可以通过在矩形形状上使用后动作绘制平行四边形来完成此操作。因此问题是:如何在后动作中访问当前节点的锚点?
编辑:以下代码显示了平行四边形是如何变形的(没有拉伸时长度不正确,拉伸时角度不正确)。它还介绍了我之前提到的方法:样式平行四边形必须传递一个现有节点作为参数,并围绕该节点绘制一个平行四边形。我希望能够在后动作中将其应用到的节点传递给它(当我写这篇文章时,我想知道是否需要后动作)。
\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}
答案1
您可以使用append after command
(或类似方法)向节点添加路径,并使用 引用节点\tikzlastnode
。
例如,使用我从阿莱南诺:
\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}
生产