Skip to content

Instantly share code, notes, and snippets.

@zr-tex8r
Created January 23, 2016 01:47
Show Gist options
  • Save zr-tex8r/2ad0368f1ce19a5474d3 to your computer and use it in GitHub Desktop.
Save zr-tex8r/2ad0368f1ce19a5474d3 to your computer and use it in GitHub Desktop.
LaTeX: to get the current position in line
% bxposinline.sty
%% declaration
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{bxposinline}[2016/01/23 v0.2]
\def\bxqpi@pkgname{bxposinline}
%% package
\RequirePackage{zref-savepos}
%--------------------------------------- main
%% variables
\newcount\bxqpi@id
\newdimen\bxqpi@indent
\newdimen\bxqpi@lwidth
\newdimen\bxqpi@offset
\let\bxqpi@label\relax
%%<+> \PILLeft/\PILRight
\newdimen\PILLeft
\newdimen\PILRight
%%<+> \GetPositionInLine
\protected\def\GetPositionInLine{%
\leavevmode \@bsphack
% do zsavepos
\bxqpi@gen@label
\zsavepos{\bxqpi@label C}%
\vadjust{\hbox{\zsavepos{\bxqpi@label L}}}%
%
\ifnum\zposx{\bxqpi@label C}=\z@ % no previous data
\PILLeft=-\maxdimen \PILRight=-\maxdimen
\else
% get paragraph parameters
\ifnum\parshape=\@ne % in LaTeX list environment
\bxqpi@indent=\parshapeindent\@ne\relax
\bxqpi@lwidth=\parshapelength\@ne\relax
\else
\bxqpi@indent=\z@ \bxqpi@lwidth=\hsize
\fi
% calculate the positions
\PILLeft=\dimexpr
\zposx{\bxqpi@label C}sp-\zposx{\bxqpi@label L}sp
-\bxqpi@indent\relax
\PILRight=\dimexpr
\bxqpi@lwidth-\PILLeft\relax
\fi
\@esphack}
%% \bxqpi@gen@label
\def\bxqpi@gen@label{%
\global\advance\bxqpi@id\@ne
\edef\bxqpi@label{bxqpi:\the\bxqpi@id}}
%--------------------------------------- done
\endinput
%% EOF
\documentclass[a4paper,twocolumn]{article}
\usepackage{bxposinline}
\def\?{\GetPositionInLine
\typeout{\the\PILLeft/\the\PILRight}}
\begin{document}
\?Lorem ipsum dolor \?sit amet,
consectetuer adipiscing elit.
Ut purus elit, vestibulum ut, placerat ac,
adipiscing vitae, felis.\?
Curabitur dictum\? \?gravida mauris.\?
\begin{itemize}
\item \?Nam arcu libero, nonummy eget,
consectetuer\? id, vulputate a, magna.
Donec vehicula augue \?eu neque.\?
\item Pellentesque habitant morbi tristique senectus et netus
et malesuada fames ac turpis egestas.
\end{itemize}
\parbox{100pt}{%
\?Mauris ut leo.
Cras\? \?viverra metus rhoncus sem.\?
Nulla et lectus vestibulum urna fringilla ultrices.
}
\end{document}
@zr-tex8r
Copy link
Author

When \GetPositionInLine is called, the length variable \PILLeft (or \PILRight) is set the hotizontal distance between the current reference point and the beginning (or end, resp.) of the current line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment