答案1
可以使用剪辑将线的一半绘制为红色:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\tikzstyle{myline} = [
ultra thick,
line cap=round,
]
\draw[
myline,
every node/.style={above, text=red, font=\sffamily},
]
(0, 0) coordinate (A) node {A}
--
(3, 1) coordinate (B) node {B}
;
\begin{scope}% limit range of clip
\clip[overlay]
($(A)!-2pt!(B)$) coordinate (tmpA)
-- ($(B)!-2pt!(A)$) coordinate (tmpB)
-- ($(tmpB)!2pt!90:(tmpA)$)
-- ($(tmpA)!2pt!-90:(tmpB)$)
-- cycle
;
\draw[myline, red] (A) -- (B);
\end{scope}
\end{tikzpicture}
\end{document}
版本,显示剪辑区域:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\tikzstyle{myline} = [
ultra thick,
line cap=round,
]
\draw[
myline,
every node/.style={above, text=red, font=\sffamily},
]
(0, 0) coordinate (A) node {A}
--
(3, 1) coordinate (B) node {B}
;
\begin{scope}[green]% limit range of clip
\draw[clip,overlay]
($(A)!-2pt!(B)$) coordinate (tmpA)
-- ($(B)!-2pt!(A)$) coordinate (tmpB)
-- ($(tmpB)!2pt!90:(tmpA)$)
-- ($(tmpA)!2pt!-90:(tmpB)$)
-- cycle
;
\draw[
myline,
red,
] (A) -- (B);
\end{scope}
\end{tikzpicture}
\end{document}