5

I always write my footnotes in LaTeX as

Some text.%
\footnote{This is the footnote text.}
Text continued.

This way the footnote is always on its own line beginning with \footnote{ and ending with } followed by newline. It should therefore be easily delimitable with a regular expression. I am rather new to Vim and am not really friends with the way regex is implemented in it.

My attempt is to have this in the .vimrc:

syntax match footnote "\\\(footnote\|footnotetext\){\zs.+\ze}\r"
highlight link footnote Comment

The idea is only to highlight the text within the \footnote. Vim does not complain but it does nothing. What am I doing wrong here?

(To have it linked with the Comment group is probably not the best idea.)

1

You probably don't want this in your .vimrc. The correct place to put this is in your after directory. See :help after-directory.

One of the last places that vimscript files get loaded from is ~/.vim/after so here we want to change a bit of syntax highlighting, so we want ~/.vim/after/syntax/. If that path doesn't exist, create it. Then create a file called ~/.vim/after/syntax/tex.vim. I'm not sure your regex is correct either, I used the following in tex.vim (though there may be a better regex):

syntax match footnote "\v\\footnote(text)?\{[^}]+}"
highlight link footnote Comment

Using this regex it doesn't matter if the footnote comes in the middle of the text or on it's own line. If for some reason you only want to highlight it if it is on it's own line, you can anchor the regex with:

syntax match footnote "\v^\\footnote(text)?\{[^}]+}$"
highlight link footnote Comment

You can open the test file and try:

:set syntax=tex

and you should see the highlighting. This worked for me but I didn't want to have to set the highlighting every time. I had to do a little more because the tex format isn't the only script being used for syntax highlighting (at least in my vim). If you open the file you want to check highlighting for, you can use :scriptnames to see what was installed. You should see a list of vim files that were loaded. In my list the last two I see are:

/home/share/vim/vim73/syntax/plaintex.vim
/home/share/vim/vim73/syntax/initex.vim

So I don't actually need ~/.vim/after/syntax/tex.vim I really need:

~/.vim/after/syntax/plaintex.vim
~/.vim/after/syntax/initex.vim

One of these has the highlighting for \footnote and the other \footnotetext. Now if I load a tex file and run :scriptnames, the last four scripts are:

/home/share/vim/vim73/syntax/plaintex.vim
/home/share/vim/vim73/syntax/initex.vim
~/.vim/after/syntax/initex.vim
~/.vim/after/syntax/plaintex.vim
  • I tried placing the first snippet in the specified file but with no result. Also I didn't understand the last part. Could you expand on it a little? – Andreas Dec 29 '14 at 14:53
  • @Andreas I added a bit more detail to the file location and using :scriptnames, is that what was missing or did you mean something else? – user1794469 Dec 31 '14 at 3:33
  • Thank you for the additional information. I am using MacVim and it loads /MacVim.app/Contnents/Resources/vim/runtime/syntax/tex.vim. Thereafter it loads my after/syntax/tex.vim. So it should show up when the tex syntax is set, right? – Andreas Dec 31 '14 at 13:35
  • @Andreas If you can see that your after/syntax/tex.vim is being sourced then the file detection sounds like it's working. You can double check your regex by copying the tex.vim file to foo.vim and then open a new file test.foo, make sure your after/syntax/foo.vim is sourced and then see if the syntax highlighting works, if not then you have a bad regex. Keep in mind that if the regex includes the closing } it won't match until you include the closing brace. – user1794469 Dec 31 '14 at 14:27
  • I did some testing and the pattern "\v(\\footnote(text)?\{)@<=.+(\}$)@=" in after/syntax/tex.vim works for a small testfile, but only after I do :set syntax=tex. It does for some reason no not work on the larger file I am currently working on. – Andreas Jan 3 '15 at 12:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.