How to Auto-Update PDF When Working on Latex | by Shinichi Okada | Oct, 2020

[ad_1]

Guide to Auto-Update PDF Using latexmk and VSCode

Photo created by creativeart — www.freepik.com

Mac:

# macOS MacTex Install
$ brew cask set up mactex-no-gui
# Updating the packages
$ sudo tlmgr replace --self && sudo tlmgr replace --all

Installation was moderately quick however it took a very long time to replace.
You might have to restart the terminal and test if works:

$ latex --version
$ latexmk --version
$ platex --version

If it doesn’t work, then you definately want to add a PATH to your ~/.zshrc or ~/.bash_profile:

export PATH="/Library/TeX/texbin/:$PATH"

Check it once more:

$ latex --version
pdfTeX 3.14159265-2.6-1.40.21 (TeX Live 2020)
kpathsea model 6.3.2
...
$ latexmk --version
Latexmk, John Collins, 29 September 2020. Version 4.70b
...
$ platex --version
e-pTeX 3.14159265-p3.8.3-191112-2.6 (utf8.euc) (TeX Live 2020)
kpathsea model 6.3.2
...

Linux set up:

# Arch Linux Family
sudo pacman -S texlive-most
# For Ubuntu, you may want a ppa:
sudo add-apt-repository ppa:jonathonf/texlive
sudo apt replace && sudo apt set up texlive-full
# Fedora
sudo dnf set up texlive-scheme-full

For Windows and different Linux OS please see this web page.

This is how to use latexmk:

$ latexmk -silent textual content.tex

It will mechanically replace the PDF file if you save a tex file. We can enhance it by modifying the .latexmkrc file in your house listing.

$ contact ~/.latexmkrc

And add the next.

#!/usr/bin/env perl# LaTeX
$latex = 'latex -synctex=1 -halt-on-error -file-line-error %O %S';
$max_repeat = 5;
# BibTeX
$bibtex = 'pbibtex %O %S';
$biber = 'biber --bblencoding=utf8 -u -U --output_safechars %O %S';
# index
$makeindex = 'mendex %O -o %D %S';
# DVI / PDF
$dvipdf = 'dvipdfmx %O -o %D %S';
$pdf_mode = 3;
# preview
$pvc_view_file_via_temporary = 0;
if ($^O eq 'linux') {
$dvi_previewer = "xdg-open %S";
$pdf_previewer = "xdg-open %S";
} elsif ($^O eq 'darwin') {
$dvi_previewer = "open %S";
$pdf_previewer = "open %S";
} else {
$dvi_previewer = "start %S";
$pdf_previewer = "start %S";
}
# clear up
$clean_full_ext = "%R.synctex.gz"
  • We use platex and dvipdfmx to convert a tex file to PDF.
  • latexmk -pv textual content.tex will show a PDF after the construct.
  • latexmk -pvc textual content.tex will replace the PDF file if you save the tex file. (You want to click on the PDF file to see the replace.)
  • You can cease expecting up to date information by ctrl+C.
VSCode extension LaTeX Workshop. Image by Author

After putting in LaTeX Workshop, you will notice the TEX icon on the finish of the left sidebar.

The LaTeX Workshop extension creates a PDF on saving and you’ll view PDF in VS Code or a browser. It has formatting, intellisense completions, syntax-highlight, snippets and shortcuts options, and extra.

LaTeX Workshop menu objects. Image by Author

You can preview PDF by clicking View LaTeX PDF.

Open the settings.json by CMD/CTRL+SHIFT+P.

VS Code CMD/CTRL+SHIFT+P. Image by Author

In the settings.json add the followings:

{
// your different settings
// ...
// ...
// ---------- Language ----------
"[tex]": {
// Enable intellisence/fast ideas
"editor.suggest.snippetsPreventQuickSuggestions": false,
// Indentation measurement
"editor.tabSize": 2
},
"[latex]": {
// Enable intellisence/fast ideas
"editor.suggest.snippetsPreventQuickSuggestions": false,
// Indentation measurement
"editor.tabSize": 2
},
"[bibtex]": {
// Indentation measurement
"editor.tabSize": 2
},
// ---------- LaTeX Workshop ----------
// Enable command and surroundings completion for used bundle
"latex-workshop.intellisense.package.enabled": true,
// goal file when deleting generated file
// Add "* .synctex.gz" to default worth
"latex-workshop.latex.clean.fileTypes": [
"*.aux",
"*.bbl",
"*.blg",
"*.idx",
"*.ind",
"*.lof",
"*.lot",
"*.out",
"*.toc",
"*.acn",
"*.acr",
"*.alg",
"*.glg",
"*.glo",
"*.gls",
"*.ist",
"*.fls",
"*.log",
"*.fdb_latexmk",
"*.snm",
"*.nav",
"*.dvi",
"*.synctex.gz"
],
// dump generated information to "out" listing
"latex-workshop.latex.outDir": "out",
// Build recipe
"latex-workshop.latex.recipes": [
{
"name": "latexmk",
"tools": [
"latexmk"
]
},
],
// Parts used within the construct recipe
"latex-workshop.latex.tools": [
{
"name": "latexmk",
"command": "latexmk",
"args": [
"-silent",
"-outdir=%OUTDIR%",
"%DOC%"
],
},
],
}

By default, intellisence/fast ideas is not going to be triggered within a snippet. To allow them set:

"editor.suggest.snippetsPreventQuickSuggestions": false,
User Snippets. Image by Author

And choose latex.json (LaTeX) from the menu.

Select latex.json. Image by Author

Let’s creates a fundamental template. This is a pattern snippet you’ll be able to add:

{
"report": {
"prefix": "report",
"body": [
"documentclass[${1:a4paper,11pt}]{${2:jsarticle}}",
"",
"",
"% Math",
"usepackage{amsmath,amsfonts}",
"usepackage{bm}",
"% image",
"usepackage[dvipdfmx]{graphicx}",
"usepackage[english]{babel}",
"$3",
"",
"begin{document}",
"",
"title{$4}",
"author{$5}",
"date{${6:today}}",
"maketitle",
"",
"",
"$0",
"",
"",
"end{document}"
],
"description": "LaTeX Sample Template"
}
}

You can add a couple of snippet:

{
"report": {
"prefix": "report",
"body": [
...
"description": "LaTeX Sample Template"
},
"note": {
"prefix": "note",
"body": [
...
"description": "Note format"
},
"exam": {
"prefix": "exam",
"body": [
...
"description": "Exam format"
}
}
A snippet in action. Image by Author
LaTeX Workshop error. Image by Author

First install the latest Perl:

# For Mac
$ perl -v
This is perl 5, version 18, subversion 2 (v5.18.2) built for darwin-thread-multi-2level
(with 2 registered patches, see perl -V for more detail)
Copyright 1987-2013, Larry Wall
...
$ brew install perl

Open a new tab or restart your terminal and check the version installed:

$ perl -vThis is perl 5, version 32, subversion 0 (v5.32.0) built for darwin-thread-multi-2levelCopyright 1987-2020, Larry Wall

You have to install the following Perl packages:

$ cpancpan[1]>set up Log::Log4perl
Running set up for module 'Log::Log4perl'
...
cpan[2]>set up Log::Dispatch::File
Running set up for module 'Log::Dispatch::File'
...
cpan[3]>set up YAML::Tiny
Running set up for module 'YAML::Tiny'
...
cpan[4]>set up File::HomeDir
Running set up for module 'File::HomeDir'
...
cpan[5]>set up Unicode::GCString
Running set up for module 'Unicode::GCString'
...

When you replace and save the tex file, it would replace the PDF file mechanically.

LaTeX menu. Image by Author.
PDF will likely be up to date if you save a tex file. Image by Author.

Now you understand how to create a PDF out of your terminal utilizing the latexmk. If you favor GUI to convert LaTeX to PDF, the LaTeX Workshop is a good instrument. You can create your personal LaTeX snippet within the VSCode.

I hope you discovered this handy and hopefully this can assist your productiveness when writing LaTeX paperwork.

[ad_2]

Source hyperlink

Write a comment