在任意位置使用 \pdfsavepos

在任意位置使用 \pdfsavepos

\pdfsavepos允许查找当前位置。我需要一行中最后一个字符末尾的位置,以便在此位置放置像盒子这样的对象。我的算法在断点(如粘连或空格)处运行良好,但我不知道如何找到连字符后的位置。有什么想法可以做到这一点吗?

我针对我的问题精简了代码。空白的位置将写入名为的文件中<jobname.co>。位置信息将在文档末尾进行分析,并在下次运行中绘制规则。

\documentclass[a4paper,12pt]{report}

\usepackage[main=english]{babel}%

\makeatletter

%

\newwrite\RJ@WriteCO%

\immediate\openout\RJ@WriteCO=\jobname.co\relax% 
open the co-file

%

\newif\ifRJ@COstarted%

%

\newenvironment{CrossOut}% delimitation of the 
region for crossing out 

 {%

  \ifvmode% output the position only in hmode

   \everypar\expandafter{\expandafter\everypar
\expandafter{\the\everypar}\the\everypar
\@CrossOut}%

   \global\let\next\relax%

  \else%

   \global\let\next\@CrossOut%

  \fi%

  \next%

 }%

 {%

  \ifRJ@COstarted% test if \CrossOut was started

   \nobreak\pdfsavepos%

   \write\RJ@WriteCO% write the end position of the 
region to the co file
{\noexpand\RJ@NewPageCO{\thepage}\noexpand
\RJ@EndPosCO%
{\the\pdflastxpos}%
{\the\pdflastypos}%
}%

  \fi%  

  \global\RJ@COstartedfalse%

 }%

%

% To avoid grouping conflicts with environments, 
the ending and new beginning

% of \CrossOut will not be done inside a group   

%   

\gdef\@CrossOut%

 {%

  \catcode`\ =13% make the blank active

  \RJ@DefBlankCO% define blanks

  \global\RJ@COstartedtrue%

  \pdfsavepos% write the starting position

  \write\RJ@WriteCO%
{\noexpand\RJ@NewPageCO{\thepage}%
 \noexpand\RJ@StartPosCO%
 {\the\pdflastxpos}{\the\pdflastypos}%
}%

 }%  



%

% Save the position of the blank

%

\def\RJ@Space{ }%

% 

{\catcode`\ =13\relax\gdef\RJ@DefBlankCO{\def 
{\RJ@SpaceCO}}}%

% 

\def\RJ@SpaceCO%

 {%

% save the current position

  \nobreak\pdfsavepos% current cursor position (baseline)

  \write\RJ@WriteCO{\the\pdflastxpos,
\the\pdflastypos}%

  \RJ@Space%

 }%


\makeatother

\begin{document}

\begin{minipage}{4,5cm}%

this \begin{CrossOut}%

is an example for cross out text. this is an example for cross out text 
verylongwordveryyylongword. this is an example for cross out%

\end{CrossOut} text.

\end{minipage}

\hspace{2cm}

\begin{minipage}{4,5cm}\raggedright%

this \begin{CrossOut}%

this is an example for cross out text. this is an example for cross out textverylongwordveryyylongword. this is an example for cross out%

\end{CrossOut} text.

\end{minipage}

\end{document}

共同文件中的注释数据:

% first minipage

\RJ@NewPageCO {1}\RJ@StartPosCO {9702016}
{46363020}% start the cross out 

10219654,46363020

11566630,46363020

14860095,46363020

16314021,46363020% position of the last blank in the first line equals eol

9557158,45412748

11322641,45412748

13537314,45412748

16314021,45412748% position of the last blank in the second line equals eol

8440600,44462476

9690251,44462476

12886393,44462476

14242994,44462476

16314021,44462476% position of the last blank in 
the third line equals eol

9035242,43512204

10974127,43512204% position of the last blank in the middle of the forth line. use eol.

13441582,42561932

15411957,42561932

16314021,425619326% position of the last blank in the fifth line equals eol

8735782,41611660

11783263,41611660

12991204,41611660

14913570,416116606% position of the last blank in the sixth line equals eol

\RJ@NewPageCO {1}\RJ@EndPosCO {16314021}{41611660}% and end of region

% second minipage

\RJ@NewPageCO {1}\RJ@StartPosCO {22058318}
{46363020} start the cross out

23303216,46363020

24077534,46363020

25147034,46363020

28163024,46363020 position of the last blank in the first line. right visible margin of this line.

21476510,45412748

23367386,45412748

24736346,45412748

26554496,45412748

28141634,45412748

28915952,45412748% position of the last blank in 
the second line after the last chracter

21369560,44462476

24385550,44462476

25562000,44462476

27452876,44462476

28821836,44462476% position of the last blank in 
the third line after the last chracter

21904310,43512204% position of the last blank in 
the middle of the forth line. use eol.

26075360,42561932

27662498,42561932

28436816,42561932% position of the last blank in the fifth line after the last chracter

21369560,41611660

24385550,41611660

25562000,41611660

27452876,41611660% position of the last blank in the sixth line after the last chracter

\RJ@NewPageCO {1}\RJ@EndPosCO {28821836}{41611660} end of region

PDF

在此处输入图片描述

如果代码中没有最后位置,我会使用 eol。它在对齐文本中工作正常,但在非对齐文本中则不行。您可以在 PDF 中看到这一点。

相关内容