colincaprani.com

Structural Engineering, Bridge Research, Programming, and more…

Entries Comments


Integrating LaTeXdiff with TeXStudio

31 August, 2017 (13:27) | Programming

Recently I’ve started using LaTeX for my more mathematical writing. Although I’ve been using MS Word + MathType for 15+ years, the frustration of bad typesetting, and the difficulty in changing formatting globally got too much for me. 

I’m using TeXStudio with MiKTeX in a Windows 10 environment (one step at a time…I’ll get to Linux eventually!).

As a postgraduate student supervisor, I make changes to text quite a bit, and MS Word’s Track Changes is very good for highlighting these changes. A tool to accomplish this in LaTeX is LaTeXdiff. Now, of course, since the LaTeX world is free, things aren’t quite as slick and user-friendly for installing as paying $100s for M$ products. So, to get LaTeXdiff up and running, took a little bit of effort, so here’s how I did it.

There are two main parts:

  1. Getting LaTeXdiff installed and working on your machine
  2. Getting a macro to automate things in TeXStudio

Good references for achieving various parts of this are:

However, not one of these worked for me in totality, and parts of the above were unnecessary, hence this post.

Installing LaTeXdiff

Very straightforward:

  1. Install the LaTeXdiff package through the MiKTeX Package Manager
  2. Install Strawberry Perl

You can verify this works by launching the “Perl (command line)” app (or just the usual cmd tool) from the Windows start menu. cd to your current directory with old.tex and new.tex versions of your file. Then execute

1
latexdiff old.tex new.tex > diff.tex

where diff.tex is the file to be written with changes marked in it. Then build the diff.tex to PDF and view something like this:

Integrating with TeXStudio

This was a bit trickier to accomplish, and took some experimentation. In TeXStudio, go to the menu Macros > Edit Macros... and add a new macro, calling it whatever you want (e.g. LaTeXdiff). Set, the macro as a Script style, and paste in the following script, which is also available to copy from PasteBin. Most of the script is for the user dialogs. At the end, it prepares a command line string, which TeXStudio executes in the shell. The stdout is then captured in readAllStandardOutputStr() and written to the diff file in the local directory. The diff file is then compiled and the PDF displayed in TeXStudio viewer.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
%SCRIPT
 
var cf = app.getCurrentFileName();
 
var ld = new String(cf);
ld=ld.substring(0,ld.lastIndexOf("/"));
 
information("Select original file");
fileChooser.setDir(ld);
fileChooser.setFilter("TeX files (*.tex)");
fileChooser.exec();
var fold=new String();
fold=fileChooser.fileName();
fold=fold.substring(fold.lastIndexOf("/")+1);
 
information("Select revised file");
fileChooser.setDir(ld);
fileChooser.setFilter("TeX files (*.tex)");
fileChooser.exec();
var fnew=new String();
fnew=fileChooser.fileName();
fnew=fnew.substring(fnew.lastIndexOf("/")+1);
 
information("Select changes tracking file");
fileChooser.setDir(ld);
fileChooser.setFilter("TeX files (*.tex)");
fileChooser.exec(fout);
var fout=new String();
fout=fileChooser.fileName();
fout=fout.substring(fout.lastIndexOf("/")+1);
 
ldfout = ld+"\\"+fout;
 
var cmdstr = new String();
cmdstr = "latexdiff-so "+fold+" "+fnew+" > /dev/null";
var proc = system("cmd /C "+cmdstr,ld);
proc.waitForFinished();
writeFile(ldfout, proc.readAllStandardOutputStr());
app.load(ldfout); // load diff file
buildManager.runCommand("txs:///quick", ldfout);
 
delete(cmdstr);
delete(dialog);
delete(fold);
delete(fnew);
delete(fout);
delete(ld);
delete(ldfout);

«

  »

Comments

Comment from Ahmed
Time: 30 May, 2018, 06:03

Thank you very much for sharing your knowledge.
The described procedure works fine from command line. However, after executing the TexStudio macro, I get the following error message:
! Emergency stop.
./diff.tex

*** (job aborted, no legal \end found)

and no output diff file is displayed.

Comment from Colin
Time: 1 June, 2018, 23:06

It could be due to a missing TeX document body: https://latex.org/forum/viewtopic.php?t=17560

Write a comment