1

I would like to add a macro, and/or a user-defined command, to the "jobname" section of my compilation command. 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 automatically filled in.

So far, I have coded a compilation command that creates a PDF file with a different name (no date):

"pdflatex" -jobname=New_Name -synctex=1 -interaction=nonstopmode %.tex

However, I have not been successful at adding a macro with the current date to the -jobname command.

I am currently using Texmaker 5.0.4 on a Mac, and I am also using PDFLaTeX to compile my document, though I am open to using XeLaTeX and LuaLaTeX.

(Some solutions to this problem that have been posted on Stack Exchange have employed other programs, such as Emacs, or they have advised solutions that do not seem to work with Texmaker; for example, the code '%m_%d_%Y' is unlikely to work with Texmaker's compiler, given that Texmaker uses the percentage sign (%) to represent the "filename without extensions.")

At any rate, I have included a sample TeX document below (though my issue is almost entirely with the commands for the compiler, so I am not sure how helpful my sample code will be):

% Input file: Original_Name.tex
% Compiled with the following command (following the four % signs):
    %%%%    "pdflatex" -jobname=New_Name -synctex=1 -interaction=nonstopmode %.tex
% Using: Texmaker 5.0.4 on Mac OS 10.13.6
% Desired output file: New_Name_{Todays_Date}.pdf (e.g., New_Name_2020_06_25.pdf)

\documentclass[12pt, letterpaper]{article}
\usepackage{csquotes}
\MakeOuterQuote{"} % Fix the backwards opening quotation mark

% Creating the underscored date format (i.e., YYYY_MM_DD):
\usepackage{datetime2}
\makeatletter
\newcommand{\yearmonthday}{%
    \@dtm@year\_\@dtm@month\_\@dtm@day
}
\makeatother


\begin{document}
Today's date: 

\verb|\yearmonthday| = \yearmonthday 
\bigskip

I would like to add the \verb|\yearmonthday| macro to the    \\ 
\verb|-jobname| section of my compilation command;           \\
e.g.: "\verb|-jobname=New_Name_\yearmonthday|" or            \\
"\verb|-jobname=New_Name_${yearmonthday}|" or some other way \\
of calling a user defined command. 

How do I specify a macro in the compilation user command?

\end{document}

Long story short:

How do I include a macro in the jobname section of Texmaker's compiler command?

(Or if you can think of a different solution for automatically including the current date (e.g., YYYY_MM_DD) in the PDF filename, then I am equally open to hearing what you might suggest!) Thank you!

6
  • Just out of curiosity, it this for version control?
    – Symbol 1
    Commented Jun 25, 2020 at 4:05
  • In a sense. Of course, I can and do use GitHub for tracking my file changes, but my question is actually motivated for drafts of a manuscript that I frequently send to colleagues and I want to know which version of the draft that I sent. I want the Tex filename to remain constant over time (properly version controlled) but I want the PDF filename to change each day that it is updated. (E.g.: Manuscript.tex and Manuscript_2020.06.25.pdf.)
    – DDCanada
    Commented Jun 25, 2020 at 4:11
  • My understanding is that it is not possible to change the (output) filename from inside a document. If I were you I would must script this. I have similar code that extracts and date stamps commits from a repository. I don't put the date into the filename but this would not be hard to do. Another option would be to set the date in the meta data for the PDF file.
    – user30471
    Commented Jun 25, 2020 at 4:21
  • So here is a method that must work but is far from beautiful. You add contents to a file called actualMS.tex; and compile the file called fakemake.tex. What fakemake.tex does is call shell-escape = "tex -jobname=proper_name acutalMS". With some fancy flags you can merge actualMS.tex and fakemake.tex into one. Just look at how the externalize library works.
    – Symbol 1
    Commented Jun 25, 2020 at 4:23
  • Feels like something that arara could handle, but I don't have much experience with it. Commented Jun 25, 2020 at 7:23

1 Answer 1

0

OK, based on the excellent advice that I received from a user on a different forum, I have a solution that addresses 95% (edit: now 100%; see update!) of the issue that I described in my initial post.

I added the following user command in Texmaker (although I suspect that the scripts for compiling documents may vary from editor to editor):

sh -c "mydate=$(/bin/date +'%%Y_%%m_%%d') ; pdflatex --jobname=New_Name_$mydate Original_Name.tex "

This solution also allows me to use PDFLaTeX, XeLaTeX, or LuaLaTeX for compiling documents. Moreover, I got the user command to work with BibTeX and the double compiler that is often required for integrating the citations and references properly. E.g.:

sh -c "mydate=$(/bin/date +'%%Y_%%m_%%d') ; xelatex -synctex=1 -interaction=nonstopmode --jobname=New_Name_$mydate Original_Name.tex ; bibtex Original_Name.aux ; xelatex -synctex=1 -interaction=nonstopmode --jobname=New_Name_$mydate Original_Name.tex ; xelatex -synctex=1 -interaction=nonstopmode --jobname=New_Name_$mydate Original_Name.tex ; open New_Name_$mydate.pdf "

Additionally, the sh -c script also allows me to add code that removes LaTeX’s compilation files after the PDF has been created:

"[...]; rm -f New_Name_$mydate.log ; rm -f New_Name_$mydate.synctex.gz ; rm -f New_Name_$mydate.aux ; rm -f Original_Name.blg "

Update:

I solved the secondary issue that I was having, about calling the Tex document's "filename without extension" (previously denoted by the '%' symbol), which wasn't working properly in the sh -c script.

According to Texmaker documentation (emphasis added):

The % character represents the name of the file without the extension (the master document in the “master” mode) and the @ character will be replaced by the current line number. Additional parameter : # will be replaced by the current file name without extension (even in the “master” mode) and ! will be replaced by the current directory.

I replaced New_Name and Original Name (from my original post) with the # symbol. And it worked! The following user command automatically uses the original filename for both the input TeX document and output PDF document:

sh -c "mydate=$(/bin/date +'%%Y_%%m_%%d') ; pdflatex --jobname=#_$mydate #.tex "

Therefore, the above code allows me to:

  1. Compile documents of filename.tex;
  2. Produce a PDF document with the filename of filename_YYYY_MM_DD.pdf;
  3. Fill in the current year, month, and day automatically; and
  4. Call the Tex document’s “filename without extension” (using the # symbol), so that my input (filename.tex) and my output files (filename_YYYY_MM_DD.pdf) are both updated automatically for all TeX documents.

Thank you to everyone who spent time thinking about and writing responses to my question. Ultimately, I now have a document that automatically produces the current date (e.g., YYYY_MM_DD) in the PDF filename without needing to edit the TeX filename! All that is required is amending the user command for compiling the final PDF document.

1
  • Another solution that requires fewer commands: sh -c "mydate=$(/bin/date +'%%Y.%%m.%%d') ; pdflatex #.tex ; cp #.pdf #_$mydate.pdf " This allows me to rename the PDF only, without renaming the other construction files (e.g., filename.aux or filename.log), by creating a copy of the output PDF document and automatically appending the current date (_YYYY_MM_DD) to it, without needing to change the .tex filename manually.
    – DDCanada
    Commented Jun 26, 2020 at 21:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .