Latex Folding and Syntax Highlighting in Vim

The syntax plugin for TeX/LaTeX that comes bundled with vim is pretty good, not only does it highlight keywords, markup, environments but allows folding of text using part/chapter/section/subsection/subsubsection/ and so on. This is a tremendous help for navigating documents.

Enabling LaTeX folding

To enable it, put

let g:tex_fold_enabled=1

in $HOME/.vim/ftplugin/tex.vim and

% vim: fdm=syntax

to the end of your LaTeX source file (or set it from the command line or add it to your .vimrc)

Vim LaTeX folding is wrong / Vim Latex Syntax Highlighting is wrong

The folding and syntax highlighting reacts badly if you have unmatched parentheses/braces/brackets in the text. If the folding works up to a certain point and then slaps everything together into one big fold or if the section markup is highlighted like a Statement instead of like a texSection — the root of the problem is probably an unclosed parenthesis.

Search for all opening parens /( and make sure they are closed.

In my case, the problem was a sad smilie face I had in a comment. :-( But I fixed it by adding a happy smilie :-) Oh, if only there were more things in life that could be fixed by adding a happy smilie!

Formatting gets really slow

Unfortunately, when the file is rather large, enabling syntax-based folding makes formatting paragraphs (e.g. gqap) really slow. I haven’t found a solution for this yet, my workaround is to temporarily set foldmethod=marker and set it back to set foldmethod=syntax when I’m not writing text.

Tagged with: , , , ,
Posted in LaTeX, Vim

BSD Games: go-fish

Sometimes back I wrote about the backgammon terminal game that comes with the bsd-games package found in many Linux distros (and *BSDs of course). Another little game I really like is the text-interface card game version of Go Fish.

It is kind of a two-player solitaire game: You try to get all four colors of a certain rank of card by asking the other player (the not-that-smart computer AI) whether it has any cards of that rank and if it does it must hand them over to you. If it doesn’t you can draw a card from the stack (standard 52 card deck) and it’s the computer’s turn.

Not that deep strategically but a nice five-minute-diversion nonetheless and actually a game you can play with kids because the rules are so simple.

Below is a transcript to show you how it looks like.

Would you like instructions (y or n)? n
I get to start.
I ask you for: 2.
You have 1 2.
I get another guess!
I ask you for: 6.
You have 1 6.
I get another guess!
I ask you for: 7.
You say "GO FISH!"

Your hand is: A 4 9 9 K
You ask me for: a
I say "GO FISH!"
You drew 10.
I ask you for: 8.
You say "GO FISH!"

Your hand is: A 4 9 9 10 K
You ask me for: 10
I have 1 10.
You get another guess!

Your hand is: A 4 9 9 10 10 K
You ask me for: 4
I say "GO FISH!"
You drew Q.
I ask you for: 9.
You have 2 9's.
I get another guess!
I ask you for: J.
You say "GO FISH!"

Your hand is: A 4 10 10 Q K
You ask me for: q
I say "GO FISH!"
You drew 9.
I ask you for: 2.
You say "GO FISH!"

Your hand is: A 4 9 10 10 Q K
You ask me for: 9
I have 3 9's.
You made a book of 9's!
You get another guess!

Your hand is: A 4 10 10 Q K + Book of 9
You ask me for: k
I say "GO FISH!"
You drew Q.
I ask you for: 5.
You say "GO FISH!"
I drew 2 and made a book of 2's!

Your hand is: A 4 10 10 Q Q K + Book of 9
You ask me for: q
I say "GO FISH!"
You drew K.
I ask you for: 6.
You say "GO FISH!"

Your hand is: A 4 10 10 Q Q K K + Book of 9
You ask me for: k
I say "GO FISH!"
You drew Q.
I ask you for: 7.
You say "GO FISH!"

Your hand is: A 4 10 10 Q Q Q K K + Book of 9
You ask me for: 4
I have 2 4's.
You get another guess!

Your hand is: A 4 4 4 10 10 Q Q Q K K + Book of 9
You ask me for: a
I say "GO FISH!"
You drew J.
I ask you for: 8.
You say "GO FISH!"

Your hand is: A 4 4 4 10 10 J Q Q Q K K + Book of 9
You ask me for: j
I have 2 J's.
You get another guess!

Your hand is: A 4 4 4 10 10 J J J Q Q Q K K + Book of 9
You ask me for: 10
I say "GO FISH!"
You drew J and made a book of J's!
I ask you for: 5.
You say "GO FISH!"

Your hand is: A 4 4 4 10 10 Q Q Q K K + Books of 9 J
You ask me for: k
I say "GO FISH!"
You drew 5.
I ask you for: 6.
You say "GO FISH!"

Your hand is: A 4 4 4 5 10 10 Q Q Q K K + Books of 9 J
You ask me for: 5
I have 3 5's.
You made a book of 5's!
You get another guess!

Your hand is: A 4 4 4 10 10 Q Q Q K K + Books of 5 9 J
You ask me for: a
I say "GO FISH!"
You drew 4 and made a book of 4's!
I ask you for: 7.
You say "GO FISH!"

Your hand is: A 10 10 Q Q Q K K + Books of 4 5 9 J
You ask me for: q
I say "GO FISH!"
You drew A.
I ask you for: 8.
You say "GO FISH!"

Your hand is: A A 10 10 Q Q Q K K + Books of 4 5 9 J
You ask me for: a
I have 1 A.
You get another guess!

Your hand is: A A A 10 10 Q Q Q K K + Books of 4 5 9 J
You ask me for: 10
I say "GO FISH!"
You drew K.
I ask you for: 6.
You say "GO FISH!"

Your hand is: A A A 10 10 Q Q Q K K K + Books of 4 5 9 J
You ask me for: k
I say "GO FISH!"
You drew 7.
I ask you for: 7.
You have 1 7.
I get another guess!
I ask you for: 8.
You say "GO FISH!"
I drew the guess!
I get to ask again!
I ask you for: A.
You have 3 A's.
I made a book of A's!
I get another guess!
I ask you for: 6.
You say "GO FISH!"

Your hand is: 10 10 Q Q Q K K K + Books of 4 5 9 J
You ask me for: q 
I say "GO FISH!"
You drew 6.
I ask you for: 7.
You say "GO FISH!"

Your hand is: 6 10 10 Q Q Q K K K + Books of 4 5 9 J
You ask me for: 6
I have 3 6's.
You made a book of 6's!
You get another guess!

Your hand is: 10 10 Q Q Q K K K + Books of 4 5 6 9 J
You ask me for: 10
I say "GO FISH!"
You drew 3.
I ask you for: 8.
You say "GO FISH!"

Your hand is: 3 10 10 Q Q Q K K K + Books of 4 5 6 9 J
You ask me for: 3
I have 3 3's.
You made a book of 3's!
You get another guess!

Your hand is: 10 10 Q Q Q K K K + Books of 3 4 5 6 9 J
You ask me for: k
I say "GO FISH!"
You drew 7.
I ask you for: 7.
You have 1 7.
I made a book of 7's!
I get another guess!
I ask you for: 8.
You say "GO FISH!"

Your hand is: 10 10 Q Q Q K K K + Books of 3 4 5 6 9 J
You ask me for: 10
I say "GO FISH!"
You drew K and made a book of K's!
I ask you for: Q.
You have 3 Q's.
I made a book of Q's!
I get another guess!
I ask you for: 8.
You say "GO FISH!"
I drew 8 and made a book of 8's!
I don't have any more cards!
My books: A 2 7 8 Q
Your books: 3 4 5 6 9 J K

I have 5, you have 7.

You win!!!
Tagged with: , ,
Posted in Linux, Linux Game

Illegal parameter number in definition of Hy@tempa

You used a # in a \url{} without escaping it. Escape it by prepending the # with a backslash, like so: \#.

If you have

\url{http://example.com/#fragment}

change it to

\url{http://example.com/\#fragment}

Carl Turner did a blog post on this error back in 2010.

Tagged with:
Posted in LaTeX, LaTeX Error

WARNING: location-reference “{}{i}” did match multiple location-classes! (taking first one)

This is the first post of a supposed number of posts on LaTeX error message. With a sufficiently large document there are a lot of Warnings and errors to deal with so I thought it might be useful to explain what they mean and — if I can — offer advice how to fix them.

This is caused by the the *.gls intermediary file of the glossaries package when rebuilding the index using xindy.

Seems harmless.

Tagged with: , , , ,
Posted in LaTeX, LaTeX Error

Rebuild PDF from VUE diagrams without GUI using latexmk

In a previous post I shared a way to export VUE diagrams to PDF in bulk on the command-line. In this post I’ll share a way to automate this even further by delegating the dynamic recreation of PDF files to LaTeX-Mk (latexmk), an excellent make-like build tool for generating documents from arbitrarily complex LaTeX projects.

latexmk can be extended using Perl in a file called latexmkrc in the project’s base directory (or one of the locations mentioned in the latexmk man page). This is accomplished by adding a custom file dependency that states that for every “*.vue* file that is newer than the corresponding “*.pdf” file (or for which noch corresponding “*.pdf” file exists) the PDF should be re-created.

Here is the code to put into latexmkrc:

add_cus_dep( 'vue', 'pdf', 0, 'makevue' );
sub makevue {
    system( "timeout 20 java -cp \$(locate VUE.jar|head -1) edu.tufts.vue.component.Export \"$_[0].vue\" \"$_[0].pdf\" pdf" );
    system( "pdfcrop \"$_[0].pdf\" \"$_[0].pdf\"" );
}

Because the edu.tufts.vue.component.Export seems to hang indefinitely for large diagrams, the process is killed after 20 seconds if it hasn’t finished before by the highly recommended timeout tool. The call to pdfcrop removes all surrounding whitespace around the actual diagram which makes the PDF files easier to include and position in LaTeX.

Now, whenever I have changed a diagram, I just have to save it and rebuild the LaTeX document, e.g. to a PDF:

$ latexmk -pdf thesis.tex

This should produce output similar to the following (for an updated VUE map diagrams/myDiagram.vue):

Latexmk: This is Latexmk, John Collins, 11 Nov. 2012, version: 4.35.
**** Report bugs etc to John Collins . ****
Latexmk: applying rule 'cusdep vue pdf diagrams/myDiagram'...
Rule 'cusdep vue pdf diagrams/myDiagram': File changes, etc:
   Changed files, or newly in use since previous run(s):
      'diagrams/myDiagram.vue'
------------
Run number 1 of rule 'cusdep vue pdf diagrams/myDiagram'
------------
For rule 'cusdep vue pdf diagrams/myDiagram', running '&do_cusdep( makevue )' ...
VUE 2013-04-04 14:59:37,106  INFO [main] Mapping: Loading mapping descriptors from jar:file:/usr/share/java/vue/VUE.jar!/tufts/vue/resources/lw_mapping_1_1.xml
VUE 2013-04-04 14:59:37,107  INFO [main] ActionUtil: unmarshalling: file:/tmp/testproj/diagrams/myDiagram.vue; charset=UTF-8
VUE 2013-04-04 14:59:37,823  INFO [main] ActionUtil: unmarshalled: LWMap[v5 myDiagram.vue n=97]
VUE 2013-04-04 14:59:38,517  INFO [main] Images: created thread pool: tufts.vue.Images$PriorityThreadPool@1a46725d[Running, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0]; maxSize=3
PDFCROP 1.38, 2012/11/02 - Copyright (c) 2002-2012 by Heiko Oberdiek.
==> 1 page written on `diagrams/myDiagram.pdf'.
Latexmk: applying rule 'pdflatex'...
Rule 'pdflatex': File changes, etc:
   Changed files, or newly in use since previous run(s):
      'diagrams/myDiagram.pdf'

Yay, another optimization as the result of procrastination!

Tagged with: , , , , ,
Posted in LaTeX

Bulk export VUE maps to PDF or image files

VUE (Visual Understanding Environment) is a fantastic tool to create graph diagrams, such as entity-relationship diagrams, visualizations of RDF data. It also has advanced features such as presentation mode where you can browse through the network of nodes semi-interactively (kinda like prezi) and it has integration with Semantic technology such as OpenCalais. VUE is awesome, if you haven’t yet, you should check it out.

In the last weeks I’ve created dozens of diagrams in VUE that visualize parts of a fairly complex data model. These diagrams are then exported to PDF and included in the LaTeX source of my thesis, which looks great because it’s vector-based, i.e. node/edge labels can be full-text searched in the final PDF and the diagrams scale smoothly when the document is zoomed in or out.

As much as I like Java, I dislike Java Swing-like GUIs — so hard to work efficiently with the keyboard (e.g. to access the menu: press F10 – then what? cursor keys don’t work…). To export a VUE map to PDF, I have to go through a ridiculous amount of mouse clicking and typing (File→Save As->Select right folder->Select Right export format->Remove “_copy” from filename->Save->Confirm overwrite). This was driving me crazy after the hundreth time I had to go through this so I searched the VUE sources and forums for a solution to automate this and – lo and behold – there is a solution!

As discussed on the VUE forums there is a command line export class in VUE called edu.tufts.vue.component.Export. Its usage is as follows:

$ java -cp $(locate VUE.jar|head -1) edu.tufts.vue.component.Export
Usage: java -jar VUEExport.jar   [option]

The arguments  are:
-h or --help  : prints this informaion
  : this is a vue map of the type .vue or .vpk extention
 : location to output file
[option]      : a number or format of the output from the following list
   			     0, jpeg or no option - saves to jpeg format
   			     1, png - saves to png format
   			     2, pdf - saves to pdf format
   			     3, html - saves to html format
   			     4, svg - saves to svg format
   			     5, rdf - saves to rdf format
   			     6, vpk - saves to vpk format

To convert a single map to PDF:

$ java -cp $(locate VUE.jar|head -1) edu.tufts.vue.component.Export my-map.vue my-map.pdf pdf

And to do it in bulk for all files in a directory, I use the following shell script:

    VUE_JAR=$(locate VUE.jar|head -1)
    max_time=10
    format="pdf"
    for i in $(ls diagrams/*vue|sort -r);do
        sans_suffix=$(echo $i|sed 's/\.vue$//')
        (java -cp $VUE_JAR edu.tufts.vue.component.Export $sans_suffix.{vue,$format} $format) & pid=$!
        (sleep $max_time && kill -9 $pid) &
        sleep $max_time && pdfcrop $sans_suffix.pdf $sans_suffix.pdf
    done

This script is a bit messy because for large maps, the export seems to hang indefinitely after successful export. THerefore I start the JVM in the background and kill it after 10 seconds. The second sleep call is there to make sure we run only one JVM at the same time. The pdfcrop command (from the TeX Live distro removes all white space around the diagram so they fit nicely into the LaTeX document.

Tagged with: , , , , ,
Posted in LaTeX

Clear all registers and macros in Vim

Vim is my favorite text editor and while it is lightweight in most respects, it tends to store a lot of information between edits about recently used registers (for macros and yanking/pasting), undo information and much more. These are great features I’ve come to rely on a lot but they can be annoying if one is working with sensitive information. Therefore I have two vim configurations: One feature-rich with lots of plugins and bling and one absolutely basic for editing sensitive files like password files or e-mail.

Today I accidentally used the full profile for lots of password and configuration editing and sure enough the registers (:reg) contained things I don’t want in my history. I could have just replaced the offending registers with something harmless interactively (to clear the register f: qfq) but I wanted a general solution.

Tim Chase provided a solution for just this problem in 2005. I split that one-liner up into a function, put it in my ~/.vimrc and just have to type :ClearRegisters in command mode whenever I used the wrong vim profile or paranoia hits :)

function! ClearRegisters()
    let regs='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789/-="*+'
    let i=0
    while (i<strlen(regs))
        exec 'let @'.regs[i].'=""' 
        let i=i+1
    endwhile
endfunction

command! ClearRegisters call ClearRegisters()
Tagged with:
Posted in Vim
Follow

Get every new post delivered to your Inbox.