Latex tikz 在路径上左对齐文本

Latex tikz 在路径上左对齐文本

我希望文本像下面第二张图片一样在行上左对齐。有没有更好的方法来复制第二张图片模板?

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric, arrows}
\begin{document}
\begin{tikzpicture}
\draw (0,0) rectangle (3,4);
\draw (0.1,3.5)-- node[anchor=south ]{$B(y)$}(2.9,3.5); %align = left in node option doesnt work
\end{tikzpicture}
\end{document}

我的结果

我努力实现的目标

答案1

这是一个可能的解决方案,但根据您的意愿,代码可以更加灵活。

在此处输入图片描述

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric, arrows}
\begin{document}
\begin{tikzpicture}
\draw (0,0) rectangle (3,4);
\draw (0.1,3.5)node[anchor=south west,inner xsep=0pt,inner ysep=1pt]{$B(y)$} -- (2.9,3.5); %align = left in node option doesnt work
\end{tikzpicture}

\medskip

\begin{tikzpicture}
\node[minimum width=3cm, minimum height=4cm,draw] (box) at (0,0){};

\node[anchor=north west, text width=3cm - 4pt, align=left, inner xsep=2pt] (head) at (box.north west){$B(y)$};

\draw ([xshift=2pt]head.south west) -- ([xshift=-2pt]head.south east);
\end{tikzpicture}

\end{document}

相关内容