如何彎曲 Tikz 節點?

如何彎曲 Tikz 節點?

這是一個非常簡單的問題。

是否可以為節點新增“曲線”效果?

考慮一個簡單的矩形節點:

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\node[draw,minimum width=3cm,minimum height=2cm]{};
\end{tikzpicture}
\end{document}

我想將其彎曲如下圖所示

在此輸入影像描述

這應該適用於所有節點形狀。

答案1

這是一種可能的解決方案,透過tikz pics.這裡,一個名為 marco 的mynode巨集定義有 6 個參數,這些參數配備了不同的 x、y 長度和曲率。最後一個是帶有曲線的文字標籤。如果根本沒有文本,請刪除程式碼中的路徑裝飾。

同樣的想法可以應用於其他形狀,例如三角形,但程式碼可能需要一些修改。以下是參數的定義

#1=color, #2=x length, #3=y height, #4=inward bend angle, #5=outward bend angle, #6=text

在此輸入影像描述

程式碼

\documentclass[border=10pt]{standalone}%{article}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing}
\usetikzlibrary{decorations.text}
\tikzset{%
pics/.cd,
mynode/.style args={#1#2#3#4#5#6}{
code={\node[inner sep=0pt,outer sep=0pt] at (0,0) (a) {};
\path[fill=#1] 
(a) to[bend right=#4] ++(#2,0) -- ++(0,#3) to[bend left=#5] ++(-#2,0) -- cycle;
\path[postaction={decorate},           % remove this path if no text decoration.
         decoration={raise=-15pt,
         text along path, text={#6},
         text align={left indent ={1cm}}}] 
(a) to[bend right=#4] ++(#2,0); 
}},
}

% #1=color, #2=x length, #3=y height, #4= inward bend  angle, #5= outward bend angle, #6=text

\begin{document}
\begin{tikzpicture}
  \pic {mynode={purple}{6cm}{-1cm}{20}{10}{This is my curved text.}};
  \pic at (0,-2) {mynode={olive}{6cm}{-2cm}{30}{20}{This is my curved text.}};
  \pic at (0,-5) {mynode={blue}{6cm}{-2cm}{40}{30}{This is my curved text.}};
\end{tikzpicture}
\end{document}

相關內容