latexdiff script for texstudio

3,037

Solution 1

I struggled a lot dealing with output redirect, as it seems to have been broken since the previous answer. system() now seems to discard anything after and including the '>' sign, even when it is in quotation in the cmd string (TeXstudio 2.12.0 on Windows 10). My solution was to redirect to /dev/null (this is supported), which makes it possible to retrieve the output of latexdiff by the readAllStandardOutputStr() on the process object returned from system() and then write it to file and compile and show the result (guesswork with buildManager).

I hope my version of the TeXstudio latexdiff script can be of use for others. Does not use 'cmd', so it might work on non-windows as well.

%SCRIPT

// Execute latexdiff-so with current file as new file
// optional magic comments in .tex file as input parameters:
//   !TeX latexdiff:original = <original file>
//   !TeX latexdiff:output = <output file>
// Requires perl and latexdiff to be installed

var cf = app.getCurrentFileName();
var ld = new String(cf);
ld=ld.substring(0,ld.lastIndexOf("/")); //Local directory
fnew = cf;

var fold = editor.document().getMagicComment("latexdiff:original");
if(fold == "")
{
fileChooser.setDir(ld);
fileChooser.setFilter("TeX files (*.tex)");
fileChooser.exec();
fold=fileChooser.fileName();
}

var fout = String(editor.document().getMagicComment("latexdiff:output"));
if(fout == "") // if path not given in magic comment
{
fileChooser.setDir(ld);
fileChooser.setFilter("TeX files (*.tex)");
fileChooser.exec();
fout=fileChooser.fileName();
}
else if(!((fout.indexOf(":") != -1) || (fout.indexOf("/")==0)))
fout = ld + '/' + fout; // if relative path, make absolute

var proc = system("latexdiff-so \"" + fold + "\" \"" + fnew + "\" > /dev/null", ld)
proc.waitForFinished();
writeFile(fout, proc.readAllStandardOutputStr());
//app.load(fout); // load diff file
buildManager.runCommand("txs:///quick", fout);
//app.load(fnew); // change back to original file

delete(proc);
delete(fold);
delete(fout);
delete(fnew);
delete(cf);
delete(ld);

Solution 2

All commands are executed directly. There is no shell involved. If you need shell functionality you have to wrap the call in a shell:

sh -c "/path/to/testscript foo > bar"

or on Windows:

cmd /C "/path/to/testscript.bat foo > bar"

See section Shell Functionality in the user manual.

Solution 3

As suggested by epR8GaYuh, here is the original source of the script.

https://www.youtube.com/redirect?v=IAWEywCx9hQ&event=video_description&q=https%3A%2F%2Fgoo.gl%2FXTsv8i&redir_token=1t3DFuFCfMZZsmqSds-otFoISsF8MTU0NTExMTU2NkAxNTQ1MDI1MTY2

Just to be clear, the code is not listed in the video. The link is given when you click on "show more" in the comments below the video.

The correct video is the one with this image for the youtube video, and then butterflies in the beginning. enter image description here

Solution 4

Christian's script didn't quite work for me. Using Windows. After quite a bit of experimentation, here's what did work:

...
var 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);

Things to note:

  • "cmd /C " needed in system call for Windows.
  • writeFile writes to TekStudio path, unless fout has global path

Solution 5

I got the following script from the link below the youtube video on "how to use Latexdiff by creating a macro", by Chas.

Nothing above worked for me, but this one worked like a charm. I am running windows10, and I installed Active Perl before creating the macro.

%SCRIPT

var cf = app.getCurrentFileName();

var ld = new String(cf);
ld=ld.substring(0,ld.lastIndexOf("/"));

information("old version");
fileChooser.setDir(ld);
fileChooser.setFilter("TeX files (*.tex)");
fileChooser.exec();
var fold=new String();
fold=fileChooser.fileName();
fold=fold.substring(fold.lastIndexOf("/")+1);

information("new version");
fileChooser.setDir(ld);
fileChooser.setFilter("TeX files (*.tex)");
fileChooser.exec();
var fnew=new String();
fnew=fileChooser.fileName();
fnew=fnew.substring(fnew.lastIndexOf("/")+1);

information("diff file by Chas");
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);
Share:
3,037

Related videos on Youtube

Hamid
Author by

Hamid

Updated on August 01, 2022

Comments

  • Hamid
    Hamid over 1 year

    I'm trying to use latexdiff script in the texstudio. What is the problem with the following script.

    %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);
    
    var cmd=new String();
    cmd="cd "+ld+" ; latexdiff-so "+fold+" "+fnew+" > "+fout+"\"";
    dialog=new UniversalInputDialog();
    dialog.add(cmd,"run command?");
    var rpl=dialog.exec();
    if(rpl!=null){
        system(cmd);
    }
    
    delete(cmd);
    delete(dialog);
    delete(fold);
    delete(fnew);
    delete(fout);
    delete(ld);
    

    When I'm trying to run this script I get this error:

    "The specified stdout redirection is not supported: "> CHANGE.tex"". Please see the manual for details."

  • Hamid
    Hamid over 7 years
    The link seems to be corrupted. Can you please give the change in the code. I'm using the windows.
  • Tim Hoffmann
    Tim Hoffmann over 7 years
    @Hamid Link fixed.
  • epR8GaYuh
    epR8GaYuh almost 5 years
    Why not linking the original source of the script, in your case the video?