How can I get the current position within the line? Or how can I find out how much space is left until the end of line? (With the help of \linewidth it is easy to deduct one value from the other)

Here is an example of what I want to achieve (the example is done with manual spacing).


I want to know the width of the red box (red box is inserted my me in the screenshot). After I know the length it is easy to do something like this:


Background: I am using the algpseudocode package to write pseudocode. The package provides a \Comment command to place comments after the lines. I want to have longer comments that span over multiple lines as shown in the screenshot.

What have I tried so far?

  • Usind the linegoal package. This worked somehow, but the value that the package determines changes (slightly) every time I compile it. It also has a strange error that the value is always wrong by a constant difference.

  • Following the answers here: How to get element position in LaTeX?. This worked so far that I get the absolute position, but I am unable to determine the relative position from that. If I could know the absolute x-position of the end of line I could determine the spacing, but how to do that?

  • Measuring everything that is positioned left of the red box manually. This works but I need to know before what will be positioned there. Using that method is cumbersome.

  • 1
    Using the tabto package, the value of \TabPrevPos contains the value of the horizontal location on the line just prior to the most recent tabbing. This can be used in the manner needed by the OP. Dec 10, 2021 at 4:23
  • Thank you for your reply. The document I needed it for, is long finished. I am not even sure where the source code is. So I can not really test your suggestion. But maybe it is useful for others.
    – masgo
    Dec 12, 2021 at 22:36

1 Answer 1


The reason linegoal changes its results is twofold: One is that it requires two compilations, after which it should stabilize; the second for the slight changes might be because it isn't returning a position but rather one relative to the remaining \linewidth, which may be changing for reasons of kerning decisions or something.

For absolute position you can use the zref subpackage zref-savepos (which linegoal uses) (And in either case you can only use pdfTex or XeTex -- no LuaTeX or DVI outputs). Then you use the following command combo:

\zsaveposx{<yourlabel>) % tells pdfTeX to save the current position (x-coord) once generated; returns nothing; can be used inline; expands variables in argument
\zposx{<yourlabel>) % returns the saved x-coord once generated, or 0? if not yet generated (not cycled thru compilation yet); also usable inline

The resulting coordinates are in sp I think, but at least an answer on column detection gives us a page midpoint (19000000) and what to test ("stop" and "start2") for comparing where you are. I used the following functions for my recent twocolumn tweaks:

\def\islcol#1{\ifnum 0#1<19000000 1\else 0\fi\relax}
\newcommand\isleftcol[1][blahlabel]{\zsaveposx{pos-#1} \islcol{\zposx{#1}}}
  • 1
    Can you please add an short compilable tex code showing your solution?
    – Mensch
    Jul 23, 2019 at 16:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.