3

According to this question, I would like to have a compiler in TeXstudio that when I use it, my PDF file name must be ''year''.myfilename.pdf (e.g. 2019.myfilename). Like <\number\year.myfilename.pdf> in LaTeX. How can I make it?

My minimal file:

%myfilename.tex
\documentclass{article}
\begin{document}
\number\year.myfilename.pdf
\end{document}

Second edit about @user187802's solution:

These are my steps:

1.

enter image description here

When I run Run Script on 4444.tex file, I get this error:

enter image description here

2.

enter image description here

When I run myRUN command on 4444.tex file, I get this error:

Error: Could not start command: myRUN

Error: Could not start command: "4444"

My batch file is copied in this path:

C:/texlive/2018/bin/win32/myrun.bat
dexteritas
  • 9,161
  • I have a solution for Windows-Batchfile too: first you have to check how your date is setup (year-first, year-center or year-last) then you have to slice it, and cherrypick the year. there's more here: http://stackoverflow.com/a/10945887/1810071 Sadly windows is non-uniform on the %date% – Teck-freak Nov 11 '19 at 20:55

2 Answers2

2

Note: This highly depends on what your goal is.

If the compilation is simple: Start with copying the compiler command. If the compiling command is using alias, you must dereference it. If it uses chaining, you must do so for each element. You then replace the jobname by the date given from your commandline. In this example, I use pdflatex as compiler:

Linux:   sh -c "pdflatex -synctex=1 -interaction=nonstopmode -jobname=$(date +'%%F').% %.tex"
Windows: cmd /C "pdflatex.exe -synctex=1 -interaction=nonstopmode -jobname=%%date%%.% %.tex"

Note that you might have to adjust the path to the pdflatex executable.

Edit: Apparantly windows cannot read the variable from instanced shells. :-(

So here's universal variation Using the write18 command (for linux use sh -c instead of cmd /C)

windows portable: cmd /C ""[txs-app-dir]"/../../texlive/2019/bin/win32/pdflatex.exe -shell-escape \immediate\write18{"[txs-app-dir]"/../../texlive/2019/bin/win32/pdflatex.exe\space -jobname=\the\year-"%"\space "%.tex"}"
windows installed: cmd /C "pdflatex.exe -shell-escape \immediate\write18{pdflatex.exe\space -jobname=\the\year-"%"\space "%.tex"}"

If the compilation is complex: You might call TeXStudio from a commandline, where the date or the jobname is setup as a variable. either do so directly or call TeXStudio from within itself over a helper-script or over the commandline. Then it might be possible to call it like this:

Linux:   sh -c "export jobname=$(date +'%%F').%; /path/to/texstudio %.tex"

The compilation would then be done by:

txs:///compile/[-jobname=$jobname]

Note that while you can export variables like this, you cant just chain it, as TeXStudio drops the variable. Also while you can save the date into a file with this: sh -c "echo '"$(date +'%%F').%"' > timestamp.txt" I have yet to find a way to read it into a variable in TeXStudios internal calls. And while you can do a lot with overwriting the jobname in the first line and setting up a magic comment for it in the second line, it's not complete and my gut tells me that it would be fairly stupid indeed.

If the goal is just to rename the PDF Do so afterwards. Just build a (chain-)command like this:

Linux:    txs:///compile | sh -c "mv %.pdf $(date +'%%F').%.pdf"
Windows:  txs:///compile | cmd /C "move %.pdf %%date%%.%.pdf"

Note we had to escape to the commandline to get the date. The %%F will give you yyyy-mm-dd. You can replace it with any combination of %%Y or (4-digit/2-digit years) %%m (2-digit month) and %%d (day) separated by dash, underscore or dot. So year-only would be %%Y. Sadly for windows the format of date is highly dependant on your regional settings. If you have Python or powershell installed, you may want to use them to get the timestamp instead.

If the goal is to abuse the jobname for Variables (Just for completenes sake. Irrelevant here.)

It might be tempting to abuse the jobname to transfer variables. Let's say you want to compile some exercises with or without solutions, and maybe for four different groups or something. For that it should be enough, to just create links to your main file, and then open and compile the links instead. Eventhough I said abuse the jobname this is fairly handy and may easily be considered a feature.

To sum it up: Rebuilding your compiler-setup with custom cmd- or sh-calls like in the first solution might be the best idea.

After that, the next best idea might be calling TeX-Studio from commandline with a variable predefined. Trying to pull that from within TexStudio would be hard, so dont.

While the approach by user187802 should work here, it might be better suited for larger problems, and/or if you have a fixed installation. (The first and third approach here works for portable setups too.)

Finally [txs-app-dir] and [txs-settings-dir] may be helpfull when using portable installations.

  • Thanks a lot my dear Teck-freak. I used this one: cmd /C "pdflatex.exe -synctex=1 -interaction=nonstopmode -jobname=%%date%%.% %.tex" my tex file name is 4444.tex but my pdf file name is %.4444.pdf. but i want to be 2019.4444.pdf or 2019-11-12.4444.pdf – SH.Madadpour Nov 11 '19 at 21:35
  • 1
    @SH.Madadpour Hey there. So aparrantly on windows if you start texstudio without commandline, the %date% variable goes missing. I have no clue, if you could circumvent this by writing a batch or by starting Texstudio from cmd-line. The universal approach should help you though. Please tell me if there's more. Also you might want to tell us, why you need the jobname changed. – Teck-freak Nov 12 '19 at 19:52
  • Thanks a lot my dear. I used this one: cmd /C "pdflatex.exe -shell-escape \immediate\write18{pdflatex.exe\space -jobname=\the\year-"%"\space "%.tex"}" It worked correctly. Excellent.Thanks again. – SH.Madadpour Nov 12 '19 at 20:15
  • You told: why you need the jobname changed? At the end of any project, I want to save it at that year. In this way I can search it quickly. – SH.Madadpour Nov 12 '19 at 20:28
  • @SH.Madadpour Fair enough. In that case on Linux I'd just rename the PDF afterwards, but since Windows doesn't spill its date, the write18-approach might be easier on Windows. Note that in pdfLaTeX write18 is a shell-escape, which often by default is closed. That's why we need the -shell-escape flag in our call. If you want to change the compiler, you have to change the inner compiler call. E.G. allow a write18 shell escape inside your file by this call: cmd /C "pdflatex.exe -shell-escape \immediate\write18{pdflatex.exe\space -shell-escape\space -jobname=\the\year-"%"\space "%.tex"}" – Teck-freak Nov 13 '19 at 06:38
1

Create a script named myRUN:

#!/bin/bash
YEAR=`date +%Y`
pdflatex -jobname=$YEAR.$1 $1

and define it in TeXstudio as a user defined command (user6 in my example):

enter image description here

Then you'll get for example for a source file zzz7.tex the output 2019.zzz7.pdf. For Windows you have to write a similiar batch skript.

user187802
  • 16,850