One year ago, I posted a question about how to add a macro to the title of the compiled PDF document, using Texmaker, and I received excellent advice from members of this forum. (I remain grateful!)
I recently purchased a new computer and I have decided to use this as an opportunity to switch from Texmaker to TexStudio. However, these two programs have different approaches to compiling TeX files into PDF documents—and, therefore, I am revisiting my question from last year about how to add a macro to the PDFLaTeX command.
What I wrote a year ago:
My ultimate goal is to add the current date (e.g., YYYY_MM_DD) to the PDF filename. For instance, if my input TeX document were named “Original_Name.tex”, I would want the output PDF file to be named “New_Name_YYYY_MM_DD.pdf” in which the current date is filled in.
The solution (by samcarter):
You can add a “user command” like this:
sh -c "mydate=$(/bin/date +'%%Y_%%m_%%d') ; pdflatex --jobname=document1_$mydate document.tex "
This will result in a pdf file document1_2020_06_25.pdf. The exact format of the date can be adjusted according to your taste, a summary of the available options can be found at https://www.man7.org/linux/man-pages/man1/date.1.html
My modification to the solution, which allows me (a) to create a second PDF with the date in the title and (b) to view the resulting PDF, all in one command line:
sh -c "mydate=$(/bin/date +'%%Y_%%m_%%d') ; pdflatex #.tex ; cp #.pdf #_$mydate.pdf "|open %.pdf
So, after creating a new copy of the dated PDF, I ended the shell command (denoted by the "), I added a ‘second’ command, using the symbol prescribed by Texmaker (|), and I opened %.pdf using Texmaker’s native command.
I have been experimenting with how to adapt this code for TexStudio, but I haven’t been successful yet. In last year’s thread, samcarter did make the following comment:
With texstudio I would simply add something like
txs:///view-pdf "?am)_$mydate.pdf"
to the command, but this obviously won’t work with texmaker…
However, I am struggling to integrate the requisite shell command (e.g., sh -c "mydate=$(/bin/date +'%%Y_%%m_%%d')
) to the user commands in TexStudio.
My question for everyone is: After compiling a TeX file with PDFLaTeX, how can I add a macro with the current date to the resulting PDF document’s filename—but this time using TexStudio?
In the end, I was able to answer my own question about how to add a macro (with the date) to the title of my PDF document in TexStudio.
First, I created a user command that copies the PDF document and renames the new PDF document with the current date:
xxxxxxxxxx
sh -c "cp %.pdf %--$(/bin/date +'%%Y.%%m.%%d').pdf "
Second, I call this line of code in a subsequent user command. E.g.:
xxxxxxxxxx
txs:///pdflatex | txs:///titledate | txs:///view
Here is a screenshot of the user commands that worked for me in TexStudio:
Perhaps this will prove useful for someone who finds this thread via a search for a related TeX question!