add tag
Anonymous 1367
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 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 not working for me 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 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!

P.S. I believe that a member who is now posting on this website once recommended to use the following code, but I have not been able to get this to work (perhaps because I am pasting it into the wrong section of Texmaker; i.e., my user commands):
```
mydate=$(date +'%m_%d_%Y') ; pdflatex --jobname="document1_$mydate" %.tex
```
Top Answer
samcarter
You can add a "user command" like this:

![Screen Shot 2020-06-25 at 12.22.48.png](/image?hash=227a37280f7a1a14a0ce7d0b49898a0b235aa87b79f428cf22c5ae82b647e326)

Here the relevant command for copy&pasting

```
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

P.S. I have no idea how to automatically update the resulting pdf in the internal viewer. 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...

Answer #2
DDCanada
I found a solution for my follow-up issue that I discussed in the sidebar comments:
> Instead of ```document1.tex``` and ```--jobname=document1_$mydate``` … can I somehow replace “document1” with the “filename without extension” (previously denoted by the ‘%’ symbol)?

After reading a ton of documentation about Bash and Shell---which are not my forté---as well as Texmaker, and how to call directories and filenames in each of these coding environments. The answer, it turns out, was a simple fix based on a throwaway line in the Texmaker documentation:
> 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.

Source: [Texmaker documentation](https://www.xm1math.net/texmaker/doc.html#SECTION02) (emphasis added).

I replaced "```New_Name```" and "```Original Name```" (from my original post), as well as "```$0```" (which the excellent @samcarter recommended in a follow-up comment) with the ```#``` symbol. And it worked! Evidently the ```#``` parameter really does *continue* to work in the "master" mode (whatever that might mean).

Therefore, this was the compilation code that worked for me in Texmaker:

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

The above code allowed 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 my question, and I am particularly grateful of @samcarter for lending expertise!
Answer #3
samcarter
Another approach could be to use [`spix`](https://ctan.org/pkg/spix?lang=en) to compile the document (with the texmaker user command `spix %`). Then one could place the following code in the file:


```
%$ mydate=$(/bin/date +'%Y_%m_%d')
%$ pdflatex -interaction=nonstopmode --jobname=$basename\_$mydate $basename

\documentclass{article}

\begin{document}
test
\end{document}
```

Enter question or answer id or url (and optionally further answer ids/urls from the same question) from

Separate each id/url with a space. No need to list your own answers; they will be imported automatically.