[Tex/LaTex] Forcing left text alignment in a fit Tikz node

horizontal alignmenttikz-pgf

I would like to add Tikz "marker" nodes in running text; then fit another node based on those "marker" nodes' positions; and insert a text into the fitted node, such that it is left aligned, and with line-breaking disabled.

Using this 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}

… I get this output:

test.png

… that is: the "marker" nodes are placed as expected; so is the fit node; but in spite of align=left, the AAAAA text is clearly centered in the fit node (there are glues left and right of the text, implying centering).

So, how can I get the AAAAA text to be left aligned in the fit node?

Best Answer

Put align=left after 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}

enter image description here