如何自动在段落和项目的开始和结束处注入命令?

如何自动在段落和项目的开始和结束处注入命令?

\item我需要修改和的定义paragraphs,以便在其开始和结束时自动插入命令。

我的用例来自于需要为 PDF 中的某些文本区域注入边界框注释。到目前为止,我在这里举了一个最简单的例子:

\usepackage{zref-savepos}
\newcounter{meas}
\def\a{\zsavepos{Start\themeas}\expandafter\pdfannot width \dimexpr\zposx{End\themeas} sp -\zposx{Start\themeas} sp +0.1cm height \dimexpr -0.1cm + \zposy{End\themeas} sp -\zposy{Start\themeas} sp depth 0.0cm {
  /Subtype /Square
  /Contents (\themeas)
}}

\def\e{\hfill\zsavepos{End\themeas}\stepcounter{meas}}

\begin{document}

\begin{itemize}
\item\a First\e
\item\a Second\e
\item\a Third - a very long line which uses more than one line of the page for some interesting reason.\e
\end{itemize}

\a This \e

\end{document}
``` 

The remaining problem now is to get the ```\a``` and ```\e``` inserted automatically and not to have to do this manually.

I have not even a clue where to start, as the definition of ```\item``` looks ...umm...strange to me. With paragraphs it is even worse...



答案1

使用最新的 LaTeX-dev (什么是“latex-dev”?),该链接已于昨天上传,您可以尝试使用新的段落钩子。请注意,在这些地方注入代码可能很棘手。阅读随附的文档texdoc ltpara

并且,请不要在未检查的情况下使用是否重新定义了\def现有命令。\a

以下示例保存位置并写入红色数字:

\documentclass{article}
\usepackage{zref-savepos}
\ExplSyntaxOn
\bool_new:N \l_dog_para_bool
\int_new:N \g_dog_para_int
\AddToHook{para/begin}
  {
   \bool_if:NT \l_dog_para_bool
     { 
       \int_gincr:N \g_dog_para_int      
       \llap
        {          
          \color_select:n{red}\tiny\int_use:N\g_dog_para_int%for debugging
          \zsavepos{start\int_use:N\g_dog_para_int}
        }
     }
  }
\AddToHook{para/end}
  {
    \bool_if:NT \l_dog_para_bool
      {
        \rlap
          {
           \zsavepos{end\int_use:N\g_dog_para_int}
           \color_select:n{red}\tiny\int_use:N\g_dog_para_int
          }
      }
  }
\NewDocumentCommand\dogmarksoff{}{\bool_set_false:N \l_dog_para_bool}
\NewDocumentCommand\dogmarkson{}{\bool_set_true:N \l_dog_para_bool}
\ExplSyntaxOff
\begin{document}

\dogmarkson
\begin{itemize}
\item First
\item Second
\item Third - a very long line which uses more than one line of the page for some interesting reason.

\end{itemize}

This 

\end{document}

在此处输入图片描述

相关内容