命令将文本放置在特定位置,无论长度

命令将文本放置在特定位置,无论长度

我有以下代码:

\documentclass[12pt]{article}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
%landmark
\draw [lightgray] (0, 0) -- (0,-1.5);

\node at (0.45,-0.2) {Hello};
\node at (1.4,-1) {Hello my friend};
\end{tikzpicture}
\end{document}

它给出以下输出:

在此处输入图片描述

我希望文本从灰色标记开始,但正如您所见,当文本长度发生变化时,x 位置也需要更改。有没有办法“自动化”这个 x 编辑过程?因为对于大型文档来说,这会变得很烦人。(也许答案在网络上的某个地方,我只是不知道如何正确地用谷歌搜索我的问题)

答案1

使用\node[anchor=west, inner xsep=0pt]

分步示例:

\documentclass[12pt]{article}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
  %landmark
  \draw [lightgray] (0, .5) -- (0,-4);

  % original, equivalent to "anchor=center, inner xsep=.3333em"
  \node at (0.45, 0) {Hello};
  \node at (1.4, -.5) {Hello my friend};
  
  % with "anchor=west" added and the x-pos of node being "0"
  \node[anchor=west] at (0, -1.5) {Hello};
  \node[anchor=west] at (0, -2) {Hello my friend};
  
  % with "inner xsep=0pt" added and syntax "\path node {...} node {...};" applied
  \path[anchor=west, inner xsep=0pt]
    node at (0, -3) {Hello}
    node at (0, -3.5) {Hello my friend};
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容