functionList for LaTeX: Trying to use classRange to have a hierarchical chapter > section document outline
-
I’m trying to use the npp function list to produce a document outline for me which shows something like the following:
Chapter 1 > Section 1.1 > Section 1.2 Chapter 2 > Section 2.1 > Section 2.2
I can’t work out why this doesn’t work:
<NotepadPlus> <functionList> <parser id="latex" displayName="LaTeX" commentExpr="(%.*?$)"> <classRange mainExpr="\\chapter\*?\{(.*?)\}"> <className> <nameExpr expr=".*"/> </className> <function mainExpr="\\section\*?\{(.*?)\}"> <functionName> <funcNameExpr expr=".*"/> </functionName> </function> </classRange> </parser> </functionList> </NotepadPlus>
As far as I can tell from the manual, that should look for
\chapter{}
or\chapter*{}
as the start of a class and with noopenSymbole
/closeSymbole
it should treat everything after the first\chapter{}
as a class range until the next\chapter{}
.Instead I get an empty function list.
I can get chapters and sections on one level ok using
\\(chapter|section)...
for the regex, but I can’t get classRange to work for the life of me. -
I’m trying to use the npp function list to produce a document outline for me which shows something like the following:
could you share some example LaTeX which would resolve to that FunctionList? It would make it easier for us to help you.
I can’t work out why this doesn’t work:
…<classRange mainExpr="\\chapter\*?\{(.*?)\}">
\*?
says “0 or 1 literal asterisk characters”. Is that really what you’re trying to match? I don’t know enough about LaTeX to make an educated guess there; it just seems odd to me. But looking at the built-inlatex.xml
, I guess that uses the\*?
after each of it’s “functions”, so maybe that is reasonable.As far as I can tell from the manual, that should look for
\chapter{}
or\chapter*{}
as the start of a class and with noopenSymbole
/closeSymbole
it should treat everything after the first\chapter{}
as a class range until the next\chapter{}
.If you don’t have
openSymbole
/closeSymbole
, then the mainExpr for the classRange needs to match the entire class, from beginning to end, and then it searches for any functions inside the results of that regex. (And it needs to be a multi-line match)Instead I get an empty function list.
Unfortunately, that’s a common occurrence when trying to debug FunctionList, especially with classes.
I can get chapters and sections on one level ok using
\\(chapter|section)...
for the regex, but I can’t get classRange to work for the life of me.(Did you start on your own, or did you start with the
latex.xml
that ships with Notepad++ since v8.7? Because that shows a bigger list of things all at the same level, which is better than nothing)I’ll see if I can come up with something, starting from the default
latex.xml
, that will get you started in the right direction. -
using the example text found here, which has chapters and sections, I am able to get something I think is reasonable.
<?xml version="1.0" encoding="UTF-8" ?> <!-- ==========================================================================\ | To learn how to make your own language parser, please check the following | link: https://npp-user-manual.org/docs/function-list/ \=========================================================================== --> <NotepadPlus> <functionList> <parser displayName="LaTeX Syntax" id ="latex_class" commentExpr="(?x) (%.*?$) # Comment " > <function mainExpr="(?x) # free-spacing (see `RegEx - Pattern Modifiers`) (?im-s) # ignore case, ^ and $ match start/end of line, dot doesn't match newline \\(begin| part\*?| subsection\*?| subsubsection\*?| paragraph\*?| subparagraph\*?) {.*}" > </function> <classRange mainExpr ="(?x) # free-spacing (see `RegEx - Pattern Modifiers`) (?m) # ^ and $ match at line-breaks (?'CLASS_START' ^ # NO leading white-space at start-of-line \\(chapter\*?) ) (?s:.*?) # whatever, (?= # ...up till \s* # ...optional leading white-space of (?: (?&CLASS_START) # ...next header | \Z # ...or end-of-text ) ) " > <className> <nameExpr expr="(?x) # free-spacing (see `RegEx - Pattern Modifiers`) \\(chapter\*?) # prefix INCLUDED { # brace before name INCLUDED .*? # name } # brace after name INCLUDED " /> </className> <function mainExpr="(?xm-s) # free-spacing (see `RegEx - Pattern Modifiers`) \\ ( section\*? |subsection\*? |subsubsection\*? |paragraph\*? |subparagraph\*? ) {.*?} " > <functionName> <funcNameExpr expr="(?xm-s) # free-spacing (see `RegEx - Pattern Modifiers`) \\ ( section\*? |subsection\*? |subsubsection\*? |paragraph\*? |subparagraph\*? ) {.*?} " /> </functionName> </function> </classRange> </parser> </functionList> </NotepadPlus>
Or, if you want to hide the
\XYZ{...}
wrappers around everything:<?xml version="1.0" encoding="UTF-8" ?> <!-- ==========================================================================\ | To learn how to make your own language parser, please check the following | link: https://npp-user-manual.org/docs/function-list/ \=========================================================================== --> <NotepadPlus> <functionList> <parser displayName="LaTeX Syntax" id ="latex_class" commentExpr="(?x) (%.*?$) # Comment " > <function mainExpr="(?x) # free-spacing (see `RegEx - Pattern Modifiers`) (?im-s) # ignore case, ^ and $ match start/end of line, dot doesn't match newline \\(begin| part\*?| subsection\*?| subsubsection\*?| paragraph\*?| subparagraph\*?) {.*}" > <functionName> <nameExpr expr="(?xm-s) # free-spacing (see `RegEx - Pattern Modifiers`) (?<={) .*? (?=}) " /> </functionName> </function> <classRange mainExpr ="(?x) # free-spacing (see `RegEx - Pattern Modifiers`) (?m) # ^ and $ match at line-breaks (?'CLASS_START' ^ # NO leading white-space at start-of-line \\(chapter\*?) ) (?s:.*?) # whatever, (?= # ...up till \s* # ...optional leading white-space of (?: (?&CLASS_START) # ...next header | \Z # ...or end-of-text ) ) " > <className> <nameExpr expr="(?x) # free-spacing (see `RegEx - Pattern Modifiers`) (?<={) # brace before name .*? # name (?=}) # brace after name " /> </className> <function mainExpr="(?xm-s) # free-spacing (see `RegEx - Pattern Modifiers`) \\ ( section\*? |subsection\*? |subsubsection\*? |paragraph\*? |subparagraph\*? ) {.*?} " > <functionName> <funcNameExpr expr="(?xm-s) # free-spacing (see `RegEx - Pattern Modifiers`) (?<={) .*? (?=}) " /> </functionName> </function> </classRange> </parser> </functionList> </NotepadPlus>
which will yield:
You can, of course, feel free to tweak it to match your desires.
(Also, note that any class (ie, chapter) that doesn’t have a function (ie, section or similar) will not be listed in the FunctionList. That’s one of the quirks of the FunctionList.)