是否存在现有的工具可以使用分配给该段落的自动段落编号来添加/更新我的 .tex 文件中的注释?
我在 Cygwin (TeX Live 2016) 上使用 pdflatex,并使用基于的自定义类memoir
,以及偏执(经过调整)用于段落编号。编号代码的核心是:
\renewcommand\theparano{\textbf{[\padzeroes[4]{\decimal{parano}}]}}
\everypar{\parindent0pt\refstepcounter{parano}%
\hbox to \paranoboxwidth{\theparano\hfill}}}
我生成了带有段落编号的 PDF,没有任何问题。就我的用例而言,我有时需要按编号引用段落。我希望能够在源中查找编号,但当然,编号不在那里。有没有可以插入它们的工具?例如,我可以运行
pdflatex foo
whatever-tool foo
并将whatever-tool
编辑foo.tex
以添加
% #42
(或任意数字)在每个段落之前。
我知道我可以将段落编号与输入行号,然后推出我自己的 perl 或 gawk whatever-tool
。但是,我想知道是否有什么已经可以做到这一点。
如果这个问题已经得到解答,而我在搜索时错过了,我深表歉意!我能找到的最接近的答案是可逆文档和TeX-PDF 审查工作流程,这两者都不是我想要的。另外,如果您需要更多源代码,请告诉我。
答案1
好吧,对于任何感兴趣的人,我使用下面的 perl 脚本编写了自己的脚本annotate-parano.pl
。
在我的特定情况下,我从使用 的修改版 parano.sty 中获取了段落/行号列表newfile
。相关部分是:
\everypar{\addtostream{@parano}{\arabic{parano} \parano@sourceline}}
\parano@sourceline
修改自哪里这个答案:
\ifnum\inputlineno=\m@ne
\let\parano@sourceline\@empty
\else
\def\parano@sourceline{\the\inputlineno}
\fi
我读到过\everypar
在 LaTeX 中进行修改可能会很冒险,但在我的用例中它运行正常。
在我的文档类中,我使用
\RequirePackage[map]{parano}
后来,又\parano
。
来源如下。
来源annotate-parano.pl
#!/usr/bin/perl -s
# annotate-parano.pl: Copyright (c) 2017 cxw. CC-BY 4.0.
# Usage: annotate-parano.pl -m=linemap.txt <file(s)>
# Produces output on stdin. Line numbers are continuous over any files listed.
# linemap.txt should have one line per marker to be added, with two fields:
# paragraph number and line number.
use v5.10;
# Load the deltas from the given marker file
my @deltas;
my @markers;
my $prevline = 1; # last line number at which a marker was added
open(my $marker_fd, $m) || die "Can't open marker file $m";
while(<$marker_fd>) {
my ($parano, $lineno)=split;
push @deltas, $lineno-$prevline;
$prevline = $lineno;
push @markers, "%\@$parano"; # or whatever marker format you want
} #foreach marker
# Mark the provided input
my $lastmarkedline = 1; # anchor for the deltas. 1 is like $prevline.
my $marker_idx = 0;
while(<>) {
if( ($marker_idx <= $#deltas) &&
( ($.-$lastmarkedline) == $deltas[$marker_idx] ) ) { # time to mark
say $markers[$marker_idx];
$lastmarkedline=$.;
++$marker_idx;
}
print;
} #foreach input line
来源parano.sty
% parano.sty, modified by cxw.
% Changes Copyright (c) 2013--2017 cxw.
%%
%% The original source files were:
%% parano.dtx (with options: `package')
%%
%% File: parano.dtx Copyright (c) 2003, CV Radhakrishnan
%% River Valley Technologies, Trivandrum, India
%% <[email protected]>
%%
%% This package may be distributed under the terms of the LaTeX Project
%% Public License, as described in lppl.txt in the base LaTeX distribution.
%% either version 1.0 or, at your option, any later version.
%%
%% $Id: parano.sty,v 1.3 2003/06/14 19:20:42 cvr Exp cvr $
%%
%%
%% Usage of the commands provided:
%%
%% \parano ==> starts para numbering
%% \parano[<number>] ==> starts the next para with number <number>
%% \endparano ==> stops para numbering
%% \prepara ==> redefine to add material that needs to appear
%% before the para number. default is null
%% \postpara ==> redefine to append material to para number.
%% default is null
%% \paranoboxwidth ==> width of paranumber box, default 2em
%%
%%
\NeedsTeXFormat{LaTeX2e}
\def\Fileversion$#1: #2 ${\gdef\fileversion{#2}}
\def\Filedate$#1: #2 #3 ${\gdef\filedate{#2}}
\Fileversion$Revision: 1.3-cw $
\Filedate$Date: 2015/06/08 00:00:00 $
\ProvidesPackage{parano}
[\filedate\space\fileversion\space Paranumber support (CVR) (CW)]
\PackageWarningNoLine{parano}
{****************************************\MessageBreak
Package parano v,\fileversion\space loaded\MessageBreak
****************************************}
% Get the current source line number. Modified from
% https://tex.stackexchange.com/a/81794/38377 by
% https://tex.stackexchange.com/users/3954/gonzalo-medina
\ifnum\inputlineno=\m@ne
\let\parano@sourceline\@empty
\else
\def\parano@sourceline{\the\inputlineno}
\fi
% Map file: if the "map" option is provided, print the line numbers and
% para numbers to a map file
\newif\if@parano@makemapfile\@parano@makemapfilefalse
\def\parano@saveline{}
\DeclareOption{map}{
\@parano@makemapfiletrue
}
\ProcessOptions\relax
\if@parano@makemapfile
\RequirePackage{newfile}
% Make the map file. Thanks to https://tex.stackexchange.com/a/10922/38377
% by https://tex.stackexchange.com/users/2674/leo-liu
\typeout{Logging paragraph numbers and line numbers to \jobname.paramap}
\newoutputstream{@parano}
\openoutputfile{\jobname.paramap}{@parano}
\def\parano@saveline{%
\addtostream{@parano}{\arabic{parano} \parano@sourceline}%
}
\fi
\newcounter{parano}
%\@addtoreset{parano}{section}
\def\prepara{}
\def\postpara{}
\newdimen\paranoboxwidth
\paranoboxwidth=3em\relax
\newdimen\parwidth
\renewcommand\theparano{\thesection.\arabic{parano}}
\newif\ifparano\paranofalse
\def\parano{\global\paranotrue\@ifnextchar[{\@@parano}{\@parano}}
\AtBeginDocument{\parwidth\parindent}
\long\def\@parano{%%
%\typeout{PARINDENT = \the\parindent----PARWIDTH = \the\parwidth}
\everypar{\parindent0pt\refstepcounter{parano}%
\parano@saveline
\hbox to \paranoboxwidth{\prepara\theparano\hfill}\postpara}}
\long\def\@@parano[#1]{%
\setcounter{parano}{#1}%
\addtocounter{parano}{-1}%
\everypar{\parindent0pt%
\refstepcounter{parano}%
\hbox to \paranoboxwidth{\prepara\theparano\hfill}\postpara}}
\def\endparano{\everypar{\parindent\parwidth}
\global\paranofalse%
\parindent\parwidth%
%\typeout{PARINDENT = \the\parindent::::PARWIDTH = \the\parwidth}
}
\def\@xsect#1{%
\@tempskipa #1\relax
\ifdim \@tempskipa>\z@
\par \nobreak
\vskip \@tempskipa
\@afterheading
\else
\@nobreakfalse
\global\@noskipsectrue
\everypar{%
\if@noskipsec
\global\@noskipsecfalse
{\setbox\z@\lastbox}%
\clubpenalty\@M
\begingroup \@svsechd \endgroup
\unskip
\@tempskipa #1\relax
\hskip -\@tempskipa
\else
\clubpenalty \@clubpenalty
\everypar{}%
\fi}%
\fi
\ifparano\@parano\fi\ignorespaces}
\def\@startsection#1#2#3#4#5#6{\everypar{}%
\if@noskipsec \leavevmode \fi
\par
\@tempskipa #4\relax
\@afterindenttrue
\ifdim \@tempskipa <\z@
\@tempskipa -\@tempskipa \@afterindentfalse
\fi
\everypar{}%
\addpenalty\@secpenalty\addvspace{\@tempskipa}
\@ifstar
{\@ssect{#3}{#4}{#5}{#6}}%
{\@dblarg{\@sect{#1}{#2}{#3}{#4}{#5}{#6}}}}
%%
%% End of file 'parano.sty'
%%