TransWikia.com

Command to put text at specific position no matter length

TeX - LaTeX Asked on February 1, 2021

I have the following code :

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}

It gives the following output :

enter image description here

I want the text to start at the gray landmark, but as you can see, when the text length’s changes, the x position need to be changed as well. Is there a way to ‘automate’ this x edit process ? Because it becomes annoying with a huge document.
(Maybe the answer is somewhere on the net and I just didn’t know how o google my question properly)

One Answer

Use node[anchor=west, inner xsep=0pt].

Step-by-step examples:

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}

enter image description here

Answered by muzimuzhi Z on February 1, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP