
我製作了一個自訂形狀,我需要在其中添加文字。但問題是我的文字不適合裝飾路徑的長度。因此,我需要一種方法來自動在兩個或三個路徑之間劃分文字。
所以,這是我的形狀,裡面的線條是我想放置文字的地方:
我使用以下程式碼產生此形狀:
\begin{tikzpicture}
% shape
\draw (0,0) ++ (45:3) arc (45:135:3);
\draw (0,0) ++ (45:5) arc (45:135:5);
\draw (45:3) -- (45:5);
\draw (135:3) -- (135:5);
%lines for text
\draw (0,0) ++ (50:3.5) arc (50:130:3.5);
\draw (0,0) ++ (50:4) arc (50:130:4);
\draw (0,0) ++ (50:4.5) arc (50:130:4.5);
\end{tikzpicture}
我計劃使用路徑裝飾將文字新增至此行:
\path[
postaction={
decorate,
decoration={
text along path,
reverse path=true,
text={very long long text, which don't fit the shape boundaries}
}
}
] (0,0) ++ (50:4.5) arc (50:130:4.5);
但如果文字很長,則無法容納所提供的空間。
所以我必須找到一種方法來計算提供的空間,然後將文字劃分在幾個路徑之間。有什麼建議麼?
答案1
您可以使用單一路徑(具有多個段)。(注意“bug”:TikZ 以相反的順序使用每個段落......)
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.text}
\begin{document}
\begin{tikzpicture}
% shape
\draw (0,0) ++ (45:3) arc (45:135:3);
\draw (0,0) ++ (45:5) arc (45:135:5);
\draw (45:3) -- (45:5);
\draw (135:3) -- (135:5);
%lines for text
\draw[blue] (0,0) ++ (50:3.5) arc (50:130:3.5);
\draw (0,0) ++ (50:4) arc (50:130:4);
\draw (0,0) ++ (50:4.5) arc (50:130:4.5);
% text along path
\path[
postaction={
decorate,
decoration={
text along path,
reverse path=true,
text={Very long long text, which don't fit the shape boundaries
and so on. Very long long text... Very long text...}
}
}
]
(0,0) ++ (50:3.5) arc (50:130:3.5)
(0,0) ++ (50:4) arc (50:130:4)
(0,0) ++ (50:4.5) arc (50:130:4.5);
\end{tikzpicture}
\end{document}
這是一個完整的解決方案,沒有reverse path
選項(以避免“錯誤”)和沒有(0,0)
(以適應邊界框中的形狀)。
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.text}
\begin{document}
\begin{tikzpicture}
% shape
\draw (45:3) arc (45:135:3)
-- (135:5) arc (135:45:5)
-- cycle;
% lines for text
\draw
(50:3.5) arc (50:130:3.5)
(50:4) arc (50:130:4)
(50:4.5) arc (50:130:4.5);
% text along path
\path[postaction={decorate,decoration={
text along path,
text={Very long long text, which don't fit the shape boundaries
and so on. Very long long text... Very long text...}
}}]
(130:4.5) arc (130:50:4.5)
(130:4) arc (130:50:4)
(130:3.5) arc (130:50:3.5);
\end{tikzpicture}
\end{document}