18

I want to compile a tex document using xelatex in LaTeX Workshop extension in visual studio code. How can I change compiler from default one to xetex? I put my current setting.json file bellow.

{
    "latex-workshop.latex.tools": [

        {
            "name": "latexmk",
            "command": "latexmk",
            "args": [
                "-synctex=1",
                "-interaction=nonstopmode",
                "-file-line-error",
                "-pdf",
                "-outdir=%OUTDIR%",
                "%DOC%"
            ],
            "env": {}
        },
        {
            "name": "lualatexmk",
            "command": "latexmk",
            "args": [
                "-synctex=1",
                "-interaction=nonstopmode",
                "-file-line-error",
                "-lualatex",
                "-outdir=%OUTDIR%",
                "%DOC%"
            ],
            "env": {}
        },
        {
            "name": "latexmk_rconly",
            "command": "latexmk",
            "args": [
                "%DOC%"
            ],
            "env": {}
        },
        {
            "name": "pdflatex",
            "command": "pdflatex",
            "args": [
                "-synctex=1",
                "-interaction=nonstopmode",
                "-file-line-error",
                "%DOC%"
            ],
            "env": {}
        },
        {
            "name": "bibtex",
            "command": "bibtex",
            "args": [
                "%DOCFILE%"
            ],
            "env": {}
        },
        {
            "name": "rnw2tex",
            "command": "Rscript",
            "args": [
                "-e",
                "knitr::opts_knit$set(concordance = TRUE); knitr::knit('%DOCFILE_EXT%')"
            ],
            "env": {}
        },
        {
            "name": "jnw2tex",
            "command": "julia",
            "args": [
                "-e",
                "using Weave; weave(\"%DOC_EXT%\", doctype=\"tex\")"
            ],
            "env": {}
        },
        {
            "name": "jnw2texmintex",
            "command": "julia",
            "args": [
                "-e",
                "using Weave; weave(\"%DOC_EXT%\", doctype=\"texminted\")"
            ],
            "env": {}
        }
    ]
}

4 Answers 4

16

Maybe all you were looking for was:

{
    "latex-workshop.latex.recipe.default": "name of recipe",
}

which sets the default build recipe for LaTeX-Workshop. But I came across this question when trying to figure out how to build my file with xelatex, which I have now figured out:

I use latexmk with -xelatex option, which I have installed on Windows via MiKTeX. I used the command line in my project folder to find what arguments got me the result I wanted. In my case, the following did the trick:

D:\path\to\project>latexmk -xelatex -outdir=out file.tex

Since this result is usually fixed for a certain project, I just override tools and recipes in

D:\path\to\project\.vscode\settings.json:

{
    "latex-workshop.latex.recipe.default": "latexmk (xelatex)",
    "latex-workshop.latex.tools": [
        {
            "name": "xelatexmk",
            "command": "latexmk",
            "args": [
                "-xelatex",
                "-outdir=out",
                "file.tex"
            ]
        }
    ],
    "latex-workshop.latex.recipes": [
        {
            "name": "latexmk (xelatex)",
            "tools": [
                "xelatexmk"
            ]
        }
    ]
}

I have chosen to just hard-code my root file and output directory in the tool, but you can also use Placeholders.

2
8

Method 1

Here is how to add using placeholders without specifying file.tex, following @mbockstael 's answer. Personally I like %DOCFILE% the most, but using something like "%DOC%.tex" is fine too.

Either in your global settings.json or the one in your workspace setting under .vscode directory, first add the tools,

"latex-workshop.latex.tools": [
        {
            "name": "xelatexmk",
            "command": "latexmk",
            "args": [
                "-xelatex",
                "-outdir=%OUTDIR%",
                "%DOCFILE%"
            ]
        },
]

Then, in the recipes, just add xelatexmk

"latex-workshop.latex.recipes": [
        {
            "name": "latexmk 🔃",
            "tools": [
                "latexmk"
            ]
        },
        {
            "name": "pdflatex ➞ bibtex ➞ pdflatex × 2",
            "tools": [
                "pdflatex",
                "bibtex",
                "pdflatex",
                "pdflatex"
            ]
        },
        // let us suppose the first two recipes are Latexmk vanilla and 
        // pdflatex twice for bibtex
        {
            "name": "latexmk (xelatex)",
            "tools": [
                "xelatexmk"
            ]
        },
]

The first method has a drawback. We have to set the LatexWorkshop's recipe to be lastUsed,

"latex-workshop.latex.recipe.default": "lastUsed",

Otherwise, the next time you want to build the same file, you have to go the extension menu to choose the xelatexmk one. So we have a better option 2 as follows.


Method 2

A better option would be simply using the magic comments, i.e., simply adding

% !TeX program = xelatex

on top of the file shall do the trick when invoking latexmk.

2
4

Here's the simplified version of @wolfrevo's answer. Setting the default recipe from the LaTeX VS Code extension is enough to set xelatex. There's no need to define xelatex from VS Code settings again since all the options exist. All we have to do is select the one we want to use.

"latex-workshop.latex.recipe.default": "latexmk (xelatex)",
2

In VS Code extension LaTex Workshop there are three interrelated settings you'll need to set:

  1. latex-workshop.latex.tools
  2. latex-workshop.latex.recipes (which refers to one of the latex-workshop.latex.tools)
  3. latex-workshop.latex.recipe.default (which refers to one of the latex-workshop.latex.recipes)

LaTeX Workshop will add for you:

  • a list of "tools" if you go to Settings and search for latex-workshop.latex.tools and click "Edit in settings.json". One of them is xelatexmk (which calls latexmk -xelatex ...):

    "latex-workshop.latex.tools": [
        ...
        {
            "name": "xelatexmk",
            "command": "latexmk",
            "args": [
                "-synctex=1",
                "-interaction=nonstopmode",
                "-file-line-error",
                "-xelatex",
                "-outdir=%OUTDIR%",
                "%DOC%"
            ],
            "env": {}
        },
        ...
    ],
    
  • a list of "recipes" if you go to Settings and search for latex-workshop.latex.recipes and click "Edit in settings.json". One of them is latexmk (xelatex) (which refers to the above mentioned tool namexelatexmk):

    "latex-workshop.latex.recipes": [
        ...
        {
            "name": "latexmk (xelatex)",
            "tools": [
                "xelatexmk"
            ]
        },
        ...
    ],
    

Then you'll need to set the default recipe.

Go to Settings and search for latex-workshop.latex.recipe.default and enter the above mentioned recipe name latexmk (xelatex). Or add it to your settings.json:

"latex-workshop.latex.recipe.default": "latexmk (xelatex)",

Further you could (a) configure the arguments and environment passed to your tool and/or (b) remove all other tools and recipes if you don't need them.

Important: Make sure you save your settings.json file before make changes in the settings dialogs. Otherwise you'll overwrite them when you save the settings.json!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .