This guide will help you set up a complete LaTeX environment on Windows with MiKTeX, VS Code, and automatic formatting with latexindent
.
- Windows 10/11
- Package manager (Chocolatey, Winget, or Scoop)
- VS Code (or install it during this process)
Choose one of the following methods:
winget install MiKTeX.MiKTeX
choco install miktex -y
scoop install miktex
- Open a new terminal and update MiKTeX:
miktex packages update
- Or use MiKTeX Console (GUI):
- Open MiKTeX Console from Start menu
- Go to "Updates" tab and install all updates
- In Settings > General, set "Package installation" to "Always install missing packages on-the-fly"
latexindent
requires Perl. Install Strawberry Perl:
winget install StrawberryPerl.StrawberryPerl
Important: Close and reopen your terminal after installation!
If you have multiple Perl installations, you might need to prioritize Strawberry Perl:
- Check which Perl is being used:
where perl
perl --version
- If the wrong Perl is being used, temporarily set the correct path:
set PATH=C:\Strawberry\perl\bin;C:\Strawberry\c\bin;%PATH%
Install the modules needed by latexindent
:
cpan YAML::Tiny
cpan File::HomeDir
cpan Log::Log4perl
cpan Log::Dispatch
Install the required LaTeX packages:
miktex packages install latexindent
miktex packages install latexmk
- Create a directory for scripts (if it doesn't exist):
mkdir C:\dev\scripts
- Create
C:\dev\scripts\latexindent.bat
:
@echo off
C:\Strawberry\perl\bin\perl.exe "%LOCALAPPDATA%\Programs\MiKTeX\scripts\latexindent\latexindent.pl" %*
- Add the scripts directory to your PATH:
setx PATH "%PATH%;C:\dev\scripts"
Note: Close and reopen your terminal after this step!
- Install VS Code (if not already installed):
winget install Microsoft.VisualStudioCode
- Install LaTeX Workshop extension:
code --install-extension James-Yu.latex-workshop
Open VS Code settings (Ctrl+,), click the JSON icon in the top right, and add:
{
"editor.wordWrap": "on",
"editor.formatOnSave": true,
// LaTeX Workshop configuration
"latex-workshop.latex.tools": [
{
"name": "latexmk",
"command": "latexmk",
"args": [
"-synctex=1",
"-interaction=nonstopmode",
"-file-line-error",
"-pdf",
"%DOC%"
]
}
],
"latex-workshop.latex.recipes": [
{
"name": "latexmk",
"tools": [
"latexmk"
]
}
],
"latex-workshop.view.pdf.viewer": "tab",
"latex-workshop.latex.clean.fileTypes": [
"*.aux",
"*.bbl",
"*.blg",
"*.idx",
"*.ind",
"*.lof",
"*.lot",
"*.out",
"*.toc",
"*.acn",
"*.acr",
"*.alg",
"*.glg",
"*.glo",
"*.gls",
"*.fls",
"*.log",
"*.fdb_latexmk",
"*.snm",
"*.nav",
"*.dvi"
],
"latex-workshop.latex.autoBuild.run": "onSave",
// LaTeX formatting configuration
"latex-workshop.formatting.latex": "latexindent",
"latex-workshop.formatting.latexindent.path": "latexindent",
"latex-workshop.formatting.latexindent.args": [
"-c", "%DIR%/", "%TMPFILE%", "-y=defaultIndent: '%INDENT%'"
],
// Language-specific formatting settings
"[latex]": {
"editor.defaultFormatter": "James-Yu.latex-workshop",
"editor.formatOnSave": true
},
"[json]": {
"editor.defaultFormatter": "vscode.json-language-features"
},
"[jsonc]": {
"editor.defaultFormatter": "vscode.json-language-features"
}
}
- Create a test file
test.tex
:
\documentclass{article}
\usepackage{amsmath}
\begin{document}
\title{LaTeX Test}
\author{Your Name}
\maketitle
\section{Introduction}
This is a test document.
\subsection{Math Test}
Here's an equation:
\begin{equation}
E = mc^2
\end{equation}
\begin{itemize}
\item First item
\item Second item
\item Third item
\end{itemize}
\begin{enumerate}
\item One
\item Two
\item Three
\end{enumerate}
\end{document}
- Open the file in VS Code
- Save it (Ctrl+S) - it should auto-format the indentation
- Build it (Ctrl+Alt+B) - it should create a PDF
- Make sure Perl is in your PATH:
perl --version
- Check if latexindent works directly:
C:\Strawberry\perl\bin\perl.exe "%LOCALAPPDATA%\Programs\MiKTeX\scripts\latexindent\latexindent.pl" --version
- Ensure
C:\dev\scripts
is in your PATH:echo %PATH%
- Check which Perl is being used:
where perl
- Temporarily override PATH:
set PATH=C:\Strawberry\perl\bin;%PATH%
- Or uninstall conflicting Perl installations
- Enable automatic installation in MiKTeX Console
- Or install manually:
miktex packages install <package-name>
- Ensure your settings.json includes:
"latex-workshop.formatting.latex": "latexindent"
- Restart VS Code after changing settings
-
Keep MiKTeX updated regularly:
miktex packages update
-
For manual formatting in VS Code:
- Right-click and select "Format Document"
- Or use keyboard shortcut:
Shift+Alt+F
-
View LaTeX Workshop output for debugging:
- View → Output → Select "LaTeX Workshop" from dropdown
-
Common LaTeX packages you might want to install:
miktex packages install amsmath amssymb graphicx hyperref babel geometry
Last updated: January 2025 Tested on: Windows 11, MiKTeX 24.1, VS Code 1.85+, LaTeX Workshop 10.0+