问题
如何将文本添加到 TikZ 图片中相对于某些对象的任意位置(甚至可能遵循其形状)?
情况
我结合了在 stackexchange 上找到的两种 Tikz 问题解决方案,即:
虽然我可以创建所需的绘图,但我并不完全理解所提供的解决方案。因此,以下 问题出现:我无法找出插入某些文本的正确方法(a 不够接近并且可以沿着曲线旋转),如下图所示:
平均能量损失
\documentclass[preview]{standalone}
\usepackage{filecontents}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}
\usetikzlibrary{intersections}
\begin{filecontents}{projection.tikz}
% tangent: https://tex.stackexchange.com/a/25940/74942
% intersection: https://tex.stackexchange.com/a/58216/74942
\begin{tikzpicture}[
tangent/.style={
decoration={
markings,% switch on markings
mark=
at position #1
with
{
\coordinate (tangent point-\pgfkeysvalueof{/pgf/decoration/mark info/sequence number}) at (0pt,0pt);
\coordinate (tangent unit vector-\pgfkeysvalueof{/pgf/decoration/mark info/sequence number}) at (1,0pt);
\coordinate (tangent orthogonal unit vector-\pgfkeysvalueof{/pgf/decoration/mark info/sequence number}) at (0pt,1);
}
},
postaction=decorate
},
use tangent/.style={
shift=(tangent point-#1),
x=(tangent unit vector-#1),
y=(tangent orthogonal unit vector-#1)
},
use tangent/.default=1
] % tangent
\draw[help lines,xstep=.5,ystep=.5] (0,0) grid (4,3); %grid
\draw [
name path=curve,
tangent=0.4
] (0,0)
to [out=60,in=180] (2,2)
to [out=0, in=160] (3,1.75)
to [out=-20, in=-120] (4,3);
\draw [blue, name path=tangent, use tangent] (-1,0) -- (2,0)
node[sloped,inner sep=0cm,below,pos=.8,
anchor=north west,
minimum height=1cm,
minimum width=1cm](N){a};
\path [name path=proj] (N.north west)% intersection
node (P) {b} -- (N.south west);
\draw [name intersections={of=curve and proj}, blue] (P.center)
--
(intersection-1);
\end{tikzpicture}
\end{filecontents}
\begin{document}
\input{./projection.tikz}
\end{document}
答案1
和安德鲁·斯旺提示我就能弄清楚:
平均能量损失
\documentclass[preview]{standalone}
\usepackage{filecontents}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}
\usetikzlibrary{decorations.text}
\usetikzlibrary{intersections}
\begin{filecontents}{projection.tikz}
% tangent: https://tex.stackexchange.com/a/25940/74942
% intersection: https://tex.stackexchange.com/a/58216/74942
% text along path: https://tex.stackexchange.com/a/22316/74942
\begin{tikzpicture}[
tangent/.style={
decoration={
markings,% switch on markings
mark=
at position #1
with
{
\coordinate (tangent point-\pgfkeysvalueof{/pgf/decoration/mark info/sequence number}) at (0pt,0pt);
\coordinate (tangent unit vector-\pgfkeysvalueof{/pgf/decoration/mark info/sequence number}) at (1,0pt);
\coordinate (tangent orthogonal unit vector-\pgfkeysvalueof{/pgf/decoration/mark info/sequence number}) at (0pt,1);
}
},
postaction=decorate
},
use tangent/.style={
shift=(tangent point-#1),
x=(tangent unit vector-#1),
y=(tangent orthogonal unit vector-#1)
},
use tangent/.default=1
] % tangent
\draw[help lines,xstep=.5,ystep=.5] (0,0) grid (4,3); %grid
\def\myshift#1{\raisebox{-2.5ex}} %text along path - height from line
\draw [
name path=curve,
tangent=0.4, % where tangent is set (percent of total curve)
postaction={decorate,
decoration={text along path,
text align=center,
text={|\sffamily\footnotesize\myshift| and maybe here too }
}
} %text command
] (0,0)
to [out=60,in=180] (2,2)
to [out=0, in=160] (3,1.75)
to [out=-20, in=-120] (4,3);
\def\myshift#1{\raisebox{1ex}} %text along path - height from line
\draw [blue,
name path=tangent,
use tangent,
postaction={decorate,
decoration={text along path,
text align=center,
text={|\sffamily\footnotesize\myshift| I want some text here }
}
} %text command
] (-1,0) -- (2,0)
node[sloped,inner sep=0cm,below,pos=.8,
anchor=north west,
minimum height=1cm,
minimum width=1cm](N){a};
\path [name path=proj] (N.north west)% intersection
node (P) {b} -- (N.south west);
\draw [ name intersections={of=curve and proj},
blue,
postaction={decorate,
decoration={text along path,
text align=center,
text={|\sffamily\tiny\myshift| and here }
}
} %text command
] (P.center)
--
(intersection-1);
\end{tikzpicture}
\end{filecontents}
\begin{document}
\input{./projection.tikz}
\end{document}