Using Visual Studio Code with Okular and synctex


Visual Studio Code is a great editor, Okular a great PDF Viewer. To make them work together, a little bit of config is necessary though.


Update: Dan MacKinlay has written a nice introduction to Visual Studio Code as a LaTeX IDE. You can find his article here.

Recently I started to use Visual Studio Code more and more when I have to write LaTeX code. Together with the LaTeX Workshop Extension, creating beautiful documents is a breeze, since it has all the features of a good IDE. Except one… Synctex 🙄

The problem with pdf.js

If you use the built in pdf viewer, Visual Studio Code (or as I have recently taken to calling it VCS 😅) uses pdf js, a web based pdf viewer written by Mozilla. Though this is sufficient for some use cases, it breaks on several occasions like rendering a math formula. I therefore started using the external viewer function of the extension to mitigate such problems (namely Okular). This leads to another problem: How to use synctex with it?

Okular has a feature to jump to the correct location of a pdf, but there is one problem: Nobody ever bothered to write down the syntax for this command. So after a long Google search and several trial and error attempts low and behold the VCS config for okular and synctex. Just copy this into your user settings.

{
    "latex-workshop.view.pdf.external.command": {
        "command": "okular",
        "args": [
            "--unique",
            "%PDF%"
        ]
    },
    // If you want the external viewer to be default, uncomment the next line
    //"latex-workshop.view.pdf.viewer": "external",
    "latex-workshop.view.pdf.external.synctex": {
        "command": "okular",
        "args": [
            "--unique",
            "%PDF%#src:%LINE%%TEX%"
        ]
    }
}
Heiko Carrasco
Heiko Carrasco
Software Engineer