Tx · LaTeX & TikZ

How to Run latexdiff from TeXstudio

Published By the 9to5Science editorial team

At a glance

  1. 1. Install latexdiff

    Install latexdiff and check that the command runs from a terminal or command prompt.

  2. 2. Create a wrapper

    Use a small script to pass the old and new TeX files to latexdiff and save its output.

  3. 3. Add a user command

    Configure TeXstudio to run the wrapper with the paths to both versions and the output file.

  4. 4. Compile the diff

    Open and compile the generated diff.tex file in TeXstudio.

TeXstudio does not need a special built-in latexdiff feature. The usual approach is to run the latexdiff program as an external command and save its output to a new .tex file. This keeps both original versions unchanged and lets you compile the marked-up comparison in TeXstudio.

The example below uses a small Python wrapper. It works on Windows, macOS, and Linux if Python and latexdiff are installed. The TeXstudio command must point to the correct locations on your computer.

Install and test latexdiff

latexdiff is a Perl program commonly distributed through TeX Live and MiKTeX. Install it using your TeX distribution’s package manager, then open a terminal or command prompt and run:

latexdiff --version

If the command is not found, install the package or make sure the directory containing latexdiff is on your system’s PATH. TeXstudio must be able to find it too; if it cannot, use the full path to the executable in the wrapper.

Create a wrapper script

Save the following as latexdiff_texstudio.py:

import subprocess
import sys
from pathlib import Path

if len(sys.argv) != 4:
    raise SystemExit(
        "Usage: latexdiff_texstudio.py OLD.tex NEW.tex OUTPUT.tex"
    )

old_file, new_file, output_file = [Path(arg).resolve() for arg in sys.argv[1:]]

try:
    result = subprocess.run(
        ["latexdiff", "--flatten", str(old_file), str(new_file)],
        check=True,
        capture_output=True,
        text=True,
        cwd=str(new_file.parent),
    )
except FileNotFoundError:
    raise SystemExit("Could not find latexdiff. Install it or use its full path.")
except subprocess.CalledProcessError as error:
    sys.stderr.write(error.stderr or str(error))
    raise SystemExit(error.returncode)

output_file.parent.mkdir(parents=True, exist_ok=True)
output_file.write_text(result.stdout, encoding="utf-8")
print(f"Wrote {output_file}")

The arguments are the older source, the newer source, and the output file, in that order. The --flatten option asks latexdiff to process files included from the main TeX file as well. This is useful for projects split across several files. For a single-file document, you can remove --flatten if you prefer.

Add the command in TeXstudio

In TeXstudio, open Options → Configure TeXstudio → Build and add a command under User Commands. The exact labels can vary slightly between versions. Use a command like this, replacing the example paths with your own:

python3 "/home/alex/scripts/latexdiff_texstudio.py" "/home/alex/paper/old.tex" "/home/alex/paper/new.tex" "/home/alex/paper/diff.tex"

On Windows, use the Python launcher if needed, for example:

py "C:\Users\Alex\scripts\latexdiff_texstudio.py" "C:\Users\Alex\paper\old.tex" "C:\Users\Alex\paper\new.tex" "C:\Users\Alex\paper\diff.tex"

Give the command a recognizable name, save the settings, and run it from TeXstudio’s Tools → User menu (or the equivalent menu in your version). The quotes matter when a path contains spaces. The wrapper itself writes the output file, so the command does not depend on shell output redirection such as >.

Worked example

Suppose your project contains paper-v1.tex and paper-v2.tex in /home/sam/article/. Set the user command to run:

python3 "/home/sam/scripts/latexdiff_texstudio.py" "/home/sam/article/paper-v1.tex" "/home/sam/article/paper-v2.tex" "/home/sam/article/diff.tex"

Running it compares the two versions and writes /home/sam/article/diff.tex. Open that file in TeXstudio and compile it. latexdiff marks additions and deletions in the generated source; the output is a comparison document, not a replacement for either original.

To compare different versions regularly, edit the two input paths in the user command before each run, or make a separate TeXstudio user command for each comparison. TeXstudio’s user-command feature is distinct from its JavaScript macro system: a macro can automate more interactive workflows, but it is unnecessary when the input files are known.

Common mistakes

  • TeXstudio reports that latexdiff cannot be found. TeXstudio may have a different PATH from your terminal. Change "latexdiff" in the Python script to the full executable path, or correct the PATH available to TeXstudio.
  • The generated file has no changes from included chapters. Keep --flatten in the command. Without it, changes in files brought in by \input or \include may not be compared as expected.
  • The diff file will not compile. Check that the two source versions use compatible document structures and that the project’s packages and image paths are available. For multi-file projects, keep the generated file in the project directory, as in the example.
  • The command breaks on paths with spaces. Put quotation marks around each path in the TeXstudio command. Do not add shell redirection; the wrapper handles writing the output.

References

  1. latexdiff package on CTAN
  2. TeXstudio User Manual

Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.