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."

up vote 2 down vote accepted

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

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.

  • The link seems to be corrupted. Can you please give the change in the code. I'm using the windows. – Hamid Jul 30 '16 at 14:41
  • @Hamid Link fixed. – Tim Hoffmann Jul 31 '16 at 12:21

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

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.