| 1 |
%%! TEX program = lualatex |
| 2 |
\ProvidesFile{rescansync.tex} [2022/07/09 v0.0.0 ] |
| 3 |
\documentclass{l3doc} |
| 4 |
\EnableCrossrefs |
| 5 |
\CodelineIndex |
| 6 |
\RecordChanges |
| 7 |
\fvset{gobble=0,tabsize=4,frame=single,numbers=left,numbersep=3pt} |
| 8 |
\usepackage{hyperref} |
| 9 |
\usepackage{csquotes} |
| 10 |
|
| 11 |
%\MakeOuterQuote{"} |
| 12 |
\begin{document} |
| 13 |
\GetFileInfo{\jobname.tex} |
| 14 |
|
| 15 |
\title{\textsf{rescansync} --- Re-scan tokens with synctex information |
| 16 |
\thanks{This file describes version \fileversion, last revised \filedate.} |
| 17 |
} |
| 18 |
\author{user202729% |
| 19 |
%\thanks{E-mail: (not set)} |
| 20 |
} |
| 21 |
\date{Released \filedate} |
| 22 |
|
| 23 |
\maketitle |
| 24 |
|
| 25 |
\begin{abstract} |
| 26 |
Allow users to execute saved code to typeset text while preserving Sync\TeX\ information. |
| 27 |
\end{abstract} |
| 28 |
|
| 29 |
\section{Simple interface} |
| 30 |
|
| 31 |
\DescribeEnv{rescansyncSaveenvPacked} |
| 32 |
Saves the environment body into the specified macro after the environment name, with additional information required to execute it later. |
| 33 |
|
| 34 |
The macro is stored in some internal format. If the user want to modify the content, use one of the advanced interface described below. |
| 35 |
|
| 36 |
Requires the \pkg{saveenv} and \pkg{currfile} package to be loaded. If you want to save the environment body using some other package, use the advanced interface. |
| 37 |
|
| 38 |
\begin{function}{\rescansyncPacked} |
| 39 |
\begin{syntax} |
| 40 |
\tn{rescansyncPacked} \meta{macro} |
| 41 |
\end{syntax} |
| 42 |
Execute the stored content in \meta{macro}. |
| 43 |
|
| 44 |
The content must be stored with \env{rescansyncSaveenvPacked} environment or similar. |
| 45 |
\end{function} |
| 46 |
|
| 47 |
To give an usage example: the following code |
| 48 |
\begin{verbatim} |
| 49 |
% the following line will save the content "123 456" |
| 50 |
% and some auxiliary information into the macro \mycontent. |
| 51 |
\begin{rescansyncSaveenvPacked}{\mycontent} |
| 52 |
123 |
| 53 |
|
| 54 |
456 |
| 55 |
\end{rescansyncSaveenvPacked} |
| 56 |
|
| 57 |
% the following line will typeset 789 as usual. |
| 58 |
789 |
| 59 |
|
| 60 |
% the following line will typeset "123 456". |
| 61 |
\rescansyncPacked{\mycontent} |
| 62 |
\end{verbatim} |
| 63 |
will typeset \enquote{789 123 456} as 3 separate paragraphs, with Sync\TeX\ information preserved. |
| 64 |
|
| 65 |
If the engine is not \LuaTeX, there will still be limited Sync\TeX\ information that points to a temporary file, but the line number is preserved. |
| 66 |
|
| 67 |
\DescribeEnv{rescansyncSaveenvghostPacked} |
| 68 |
The usage of this environment is similar to the \env{rescansyncSaveenvPacked} above, except that the content is still typeset with Sync\TeX\ information preserved while it's stored. |
| 69 |
|
| 70 |
In the example above, if this environment is used instead |
| 71 |
\begin{verbatim} |
| 72 |
\begin{rescansyncSaveenvghostPacked}{\mycontent} |
| 73 |
123 |
| 74 |
|
| 75 |
456 |
| 76 |
\end{rescansyncSaveenvghostPacked} |
| 77 |
|
| 78 |
789 |
| 79 |
|
| 80 |
\rescansyncPacked{\mycontent} |
| 81 |
\end{verbatim} |
| 82 |
the content typeset will be \enquote{123 456 789 123 456}. |
| 83 |
|
| 84 |
As with \env{saveenvghost} environment (read \pkg{saveenv} package documentation for more details), the Sync\TeX\ information of the first section is guaranteed to be preserved, but there might be some performance hit. |
| 85 |
|
| 86 |
\section{Advanced interface} |
| 87 |
|
| 88 |
\begin{function}{\rescansync:nn} |
| 89 |
\begin{syntax} |
| 90 |
\cs{rescansync:nn} \Arg{content} \Arg{line offset} |
| 91 |
\end{syntax} |
| 92 |
Execute (rescan) the \Arg{content}. Requires \pkg{currfile}. |
| 93 |
|
| 94 |
Details: \Arg{content} will be detokenized, and characters with char code 10 will be interpreted as a line break |
| 95 |
(the behavior is inherited from |\iow_now:Nn| function. As a consequence, it's not allowed to write literal character with char code 10 to the file; |
| 96 |
however this is not very useful regardless because on some operating systems this is equivalent to a real newline) |
| 97 |
|
| 98 |
\Arg{line offset} is some non-negative number. If it's 0 then the first line of \Arg{content} corresponds to the first line in the target file. |
| 99 |
|
| 100 |
Remark: If \pkg{newverbs}, \pkg{xparse} or \pkg{filecontentsdef} is used to collect multiline verbatim environment, |
| 101 |
they have the line separation characters separated by character with char code 13 by default, you need to |
| 102 |
manually replace them. |
| 103 |
|
| 104 |
Even though the Sync\TeX\ information points to the correct file, if there's some error the temporary file name (which has the form |
| 105 |
\texttt{RS\meta{number}-\meta{file name}} will be shown. |
| 106 |
|
| 107 |
Engines other than \LuaTeX\ has the limitations described above. |
| 108 |
\end{function} |
| 109 |
|
| 110 |
\begin{function}{\rescansync:nnn} |
| 111 |
\begin{syntax} |
| 112 |
\cs{rescansync:nnn} \Arg{content} \Arg{line offset} \Arg{file name} |
| 113 |
\end{syntax} |
| 114 |
|
| 115 |
Similar to above, but use \Arg{file name} as the temporary file name. (only important in error messages, the Sync\TeX\ information |
| 116 |
points to the file that the content is scanned.) |
| 117 |
\end{function} |
| 118 |
|
| 119 |
\begin{function}{\rescansync:nnnn} |
| 120 |
\begin{syntax} |
| 121 |
\cs{rescansync:nnnn} \Arg{content} \Arg{line offset} \Arg{file name} \Arg{Sync\TeX\ tag} |
| 122 |
\end{syntax} |
| 123 |
|
| 124 |
Similar to above, but you're allowed to specify the Sync\TeX\ tag. |
| 125 |
|
| 126 |
More details: each file ever read has an associated Sync\TeX\ tag value, which is a number. The Sync\TeX\ information will point to that file. |
| 127 |
|
| 128 |
On engines other than \LuaTeX, this feature is not supported and the function returns empty. |
| 129 |
\end{function} |
| 130 |
|
| 131 |
\begin{function}[EXP]{\rescansync_gettag:} |
| 132 |
\begin{syntax} |
| 133 |
\cs{rescansync_gettag:} |
| 134 |
\end{syntax} |
| 135 |
|
| 136 |
Function that fully expands to some token list that represents the Sync\TeX\ tag that corresponds to some file. |
| 137 |
|
| 138 |
Note that if a file is |\input| more than once (or if |\file_get:nnN| \pkg{expl3} function is used on that file, which internally uses the \TeX\ |\input| primitive), |
| 139 |
the tag value will be different and forward search may fail to work. |
| 140 |
\end{function} |
| 141 |
|
| 142 |
As a full example, assume there's a file named \file{a.tex}: |
| 143 |
\begin{verbatim} |
| 144 |
\tl_set:Nx \mytag {\rescansync_gettag:} |
| 145 |
\end{verbatim} |
| 146 |
After it's executed, a file \file{b.tex} executes: |
| 147 |
\begin{verbatim} |
| 148 |
\rescansync:nnnV {abcdef} {3} {rescanned-a} \mytag |
| 149 |
\end{verbatim} |
| 150 |
then the text |abcdef| will be typesetted, the Sync\TeX\ information points to line $1+3=4$ in the file \file{a.tex} |
| 151 |
(as the code |abcdef| is on the first line of the \Arg{content}, and the line offset is 3). |
| 152 |
|
| 153 |
If there's any error while typesetting |abcdef|, the error file name will be reported as for example, \file{RS1-rescanned-a.tex} (the number |1| might vary) instead of \file{a.tex}. |
| 154 |
|
| 155 |
\StopEventually{% |
| 156 |
\PrintChanges |
| 157 |
\PrintIndex |
| 158 |
} |
| 159 |
\Finale |
| 160 |
\end{document} |
| 161 |
|
| 162 |
|