Regular expressions in WinEdt: the search for formulas with unused rooms

After a more detailed examination of the manual of the editor of WinEdt (used almost exclusively for creating LaTeX documents), and opened additional possibilities of the tool search/replace of this program. To activate smart search, put a checkmark in the checkbox Regular Expressions in the menu Find or Find and Replace, resulting in the search string will become, in fact, in the command line, which you can use to perform miracles. That is to make the text will be practically all, another question that sometimes too perverted (which is why in the case of serious problems the creation of appropriate macro seems more appropriate.)

the Joke about the gynecologist
Gynecologist comes in to get a job in auto repair. He is asked to assemble-disassemble the engine. It performs and is interested in the evaluation of their work. His answer: "in principle, nothing, but that's the first time we see that all this was done through the exhaust pipe".

Here is an example. Need to find all unused labels \label, that is, those for which the text no ref-links (all labels that are never referred to, as it says in the English manual). Excess label is harmless in itself, may signal, in particular, about the excessive "zanemarivanja" formulas matejovskeho document (i.e., there are formulas with unused rooms). If the text is large enough and has many numbered ratios, the occurrence of these marks is almost inevitable (you once referred to this equation, then changed the text by deleting the link, and to clean the room from the equation, most likely, forgot). However, manual detection of "extra" labels becomes (again due to the large volume of material) is too cumbersome and, most importantly dull mechanical work, the nature of which just cries out for a rational alternative.

So, solve the task using "smart" search the WinEdt editor (version 5.3 obviously should be enough). First of all, notice that WinEdt reserved memory locations (registers) with names %!0, ..., %!9 for your custom needs. And we must bear in mind that this memory is substantially operative in the sense that it is reset each time you restart WinEdt. Use this memory to save the contents of all references \ref in a single long row: press ctrl+F, don't forget about the checkbox Regular Expressions menu in the search string which you enter the following text:

the
\\ref\(\{*\}\)\X{\"|GetTag(0,0);LetReg(1,"%!1%!0");|}



Some explanations (in part reflects the meaning of the last line). When this mode is enabled Regular Expressions some characters (such as\, {, and }) acquire service sense; if they need us in their immediate value (that is, as corresponding characters), they should be used together with a slash in front (e.g. \\, \{ and \}). But there are exceptions: for example, parentheses are not special symbols (thereby meaning literally parentheses), but in combinations \( and \), on the contrary, have a special meaning. Text enclosed between \( and \), is transformed into the so-called tag (expression tag or tagged text) and can be used in further appeal to this text (for example, in the same search string to detect duplicate or fragment in the line "replace with") by the command \0 (zero is the default number saahiraani fragment). If there is a need to select multiple parts, you must use design view:

the
\(0 some text \), ..., \(9 some text \)

and the command \0, ..., \9 to refer to the relevant parts.

What's the asterisk * between \{ and \} at the beginning of the entered text? This asterisk is called a template and refers to an arbitrary sequence of characters (including empty) in the same row (note that, starting with version WinEdt 5.3, the combination of ** encodes any text, including line breaks).
Thus, the set of characters:

the
\\ref\(\{*\}\)

that is the first part of the expression searches for any combination of \ref{the arbitrary text}. When it detects this combination, run macros, as evidenced by the second part of the expression that starts with \X (in the absence of the start command macros WinEdt just goes to the found combination, highlighting it in the text of the document). And the command macros can start with \x (case matters!), and \Xx and \xX. The fact is that depending on the results of execution of macros WinEdt can go to the found text (in our case it is \ref{the arbitrary text}), and ignore it (as if it differed from the specified in the search line), navigating to the search for the next match. And what of these two alternatives he prefers, is determined by the register “x-team” and value used WinEdt Boolean variable IFOK (default is true), which is part of the macros can change. If the command \X response of WinEdt is consistent with the value of IFOK: if the value of IFOK's still the truth, WinEdt moves to the found text; if it is lies, WinEdt ignores the fragment. If the command \x response of WinEdt on the value of IFOK opposite, and when using \Xx or \xX, WinEdt regardless of the value of IFOK displays the text found.

Let us consider the second part of the parsed string, i.e. the command:

the
\X{\"|GetTag(0,0);LetReg(1,"%!1%!0");|}

She runs two macros: GetTag(0,0) and LetReg(1,"%!1%!0"). Macro GetTag(n,m) writes the contents of the n-th tag (in our case, the null tag, i.e. the argument of the \ref command with just plain curly braces) in the m-th register, i.e. a memory location with the name %!m (in our case – with the name %!0). Macro LetReg(k,"string") writes the k-th cell of its second argument (without enclosing quotes). It turns out that in our case LetReg overwrites the first register (it's nothing), adding to it the contents of the 0-th register, i.e. enclosed in curly braces argument discovered by the search WinEdt team \ref. Thus, if you are interested in cell %!0 the sequence of all arguments of the commands \ref, used in the text, by entering a search string:

the
\\ref\(\{*\}\)\X{\"|GetTag(0,0);LetReg(1,"%!1%!0");|}

to search through the entire document. This is done relatively simply and quickly: after the first successful detection of a given text search of all subsequent occurrences carried out by pressing and holding the F3 key (for a document containing many hundreds numbered ratios, hold F3 had no more than 30 seconds). However, there's another alternative — you can use the replace tool of the editor WinEdt: press ctrl+R in the search bar type in:

the
\\ref\(\{*\}\)\X{\"|GetTag(0,0);LetReg(1,"%!1%!0");|}

in the line "replace with":

the
\ref\(\{*\}\)

when prompted to confirm replace select All and forward (don't forget about the checkbox Regular Expressions).

Preparatory work completed. Now find unused label is the calling search expression:

the
\\label{\{\(*\)\}}\x{FindInString("%!1","\0");}

in the search box (think of when \x c IFOK!). Search argument:

the
\\label{\{\(*\)\}}\X{FindInString("%!1","\0");}

solves a reverse question, by showing only those labels that appear in the argument of at least one of ref-commands. Notice that the presence of the external braces in the expression: \\label{\(\{*\}\)} is syntactically superfluous, but in their absence the search for WinEdt gives, generally speaking, incorrect result. This feature has no rational explanation — it must be remember (in the English manual just says that it is important to use: {\{\(*\)\}} because \{\(*\)\} will not work here!).

Joke about a Georgian school
Teacher at the Russian language lesson in a Georgian school: "Children, remember the words of Sol, bean and vermishel are written with a soft sign, and the words Vilk, Bulka and tarelka – no. It is inexplicable and you just have to remember!"


the
... \label{h1} ... \label{h2} ... \label{1h} ... \label{h3} ... \ref{h1} ... \ref{h2} ... \label{h}

(if instead of \\ref\(\{*\}\) use \\ref\{\(*\)\}, not including { and } in saahiraani fragment, the contents of the reference forms line: h1h2, search for which will give a false result on the use of labels with the names h and 1h). This, however, does not relieve us from all possible errors, because the arguments are labels and references can themselves contain (of course, a pair of) braces (e.g., \label{h{1}}). To exclude any misunderstandings, the easiest way to abandon the use of curly brackets when naming links; if you managed to create a huge document with lots of links whose names are the data brackets, without special macro, perhaps, not enough.

Thus, the method presented here allows (to the given the above caveat) to detect all cases where any environment, generating a number (e.g., equation) contains unused labels \label. But the "extra" room can appear when the environment does not contain a \label. Fortunately, using the mechanisms of "advanced" search WinEdt is easy to detect (Search for field):

the
\\begin\{equation\}\(**\)\end\{equation\}\x{FindInString("\0","\label");}

and even fix (Replace with field):

the
\begin\{equation\*\}\to 0\end\{equation\*\}

all such misunderstandings (for definiteness, the case of the above-mentioned equation environment).
Article based on information from habrahabr.ru

Комментарии

Популярные сообщения из этого блога

When the basin is small, or it's time to choose VPS server

Performance comparison of hierarchical models, Django and PostgreSQL

From Tomsk to Silicon Valley and Back