在适合的 Tikz 节点中强制左文本对齐?

在适合的 Tikz 节点中强制左文本对齐?

我想在运行的文本中添加 Tikz“标记”节点;然后根据这些“标记”节点的位置拟合另一个节点;并将文本插入到适合的节点中,使其左对齐,并禁用换行。

使用此 MWE:

\documentclass{book}
\usepackage{trace}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{fit}
\usepackage{lua-visual-debug}

\begin{document}

% \tikz\node[fill=red](end marker){}; % same as at(0,0)
% \tikz\coordinate(end marker) at (0,0);
% must have overlay, remember picture for the fit to work

Lorem ipsum dolor sit amet,
consectetur adipis-icing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation
ullamco $laboris$ nisi ut aliquip ex ea commodo conse-
\tikz[overlay, remember picture]\node[fill,minimum size=2pt,inner sep=0pt,outer sep=0pt](begin marker)at(0,0){};%
quat. Duis aute irure dolor in reprehenderit
\tikz[overlay, remember picture]\node[fill=red,minimum size=2pt,inner sep=0pt,outer sep=0pt](end marker)at(0,0){};%
in voluptate velit esse cillum dolore eu fugiat nulla pariatur.


\begin{tikzpicture}[overlay, remember picture]
  \traceon %
  \node%
  [%
    align=left,
    anchor=south west,
    inner sep=0pt,
    outer sep=0pt,
    minimum size=0pt,
    %text width=, % "Setting dimension to an empty string causes the automatic line breaking to be disabled."
    text=red,
    % hand tuned for best position:
    fit={($(begin marker)+(-1pt,0pt)$)($(end marker)+(0,-2pt)$)},
  ]%
  %at ($(begin marker)+(0,2.5pt)$) % don't use at() if using fit!
    {AAAAA}%
  ;
  \traceoff %
\end{tikzpicture}
\end{document}

...我得到了这个输出:

测试.png

... 即:“标记”节点按预期放置;适合节点也是如此;但尽管如此align=left,AAAAA 文本仍清楚地位于适合节点的中心(文本左右两侧有胶水,暗示居中)。

那么,如何让 AAAAA 文本在 fit 节点中左对齐?

答案1

放在align=left之后fit = {...}

\documentclass{book}
\usepackage{trace}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{fit}
\usepackage{lua-visual-debug}

\begin{document}

% \tikz\node[fill=red](end marker){}; % same as at(0,0)
% \tikz\coordinate(end marker) at (0,0);
% must have overlay, remember picture for the fit to work

Lorem ipsum dolor sit amet,
consectetur adipis-icing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation
ullamco $laboris$ nisi ut aliquip ex ea commodo conse-
\tikz[overlay, remember picture]\node[fill,minimum size=2pt,inner sep=0pt,outer sep=0pt](begin marker)at(0,0){};%
quat. Duis aute irure dolor in reprehenderit
\tikz[overlay, remember picture]\node[fill=red,minimum size=2pt,inner sep=0pt,outer sep=0pt](end marker)at(0,0){};%
in voluptate velit esse cillum dolore eu fugiat nulla pariatur.


\begin{tikzpicture}[overlay, remember picture]
  \traceon %
  \node%
  [%
    anchor=south west,
    inner sep=0pt,
    outer sep=0pt,
    minimum size=0pt,
    %text width=, % "Setting dimension to an empty string causes the automatic line breaking to be disabled."
    text=red,
    % hand tuned for best position:
    fit={($(begin marker)+(-1pt,0pt)$)($(end marker)+(0,-2pt)$)},align=left,
  ]%
  %at ($(begin marker)+(0,2.5pt)$) % don't use at() if using fit!
    {AAAAA}%
  ;
  \traceoff %
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容