90

I have a source file called 2011-05-27_Myfilename.tex and I'd like to create a PDF file from it called only Myfilename.pdf.

Can this be configured from within the .tex file itself?

(It seems to be possible with

pdflatex -jobname=Myfilename.tex 2011-05-27_Myfilename.tex

However, as many different files are concerned, It would be easier to have an option like output=Myfilename in the LaTeX source code.)

4
  • 5
    I'm used to making Makefile scripts to generate my PDFs. If that is your case, too, it's easy to enough to either rename the .tex or the .pdf in the process.
    – raphink
    May 27, 2011 at 7:23
  • 1
    @Raphink: Thanks, that Makefile approach sounds like a good workaround, however I did not use it yet. As I'm using GUI editors it is quite easy to use the keyboard shortcut to run pdflatex. May 27, 2011 at 7:28
  • 3
    If you have a source file called <date>_myfilename.tex, that probably means you aren't using a version control system. I highly recommend you take a few minutes to learn one such as git, it will make your life easier and better in the long run. Once you get the hang of it you'll never go back to manually saving incremental copies of a file.
    – Caleb
    Jul 21, 2018 at 13:20
  • @Caleb: thank you, good point. I was asking that question 7 years ago, so I don't remember the circumstances exactly, but I have been using a VCS for years now and you're right, it is great and very helpful. (However it is still sometimes difficult for me as a non-programmer to understand the details of it.) Jul 30, 2018 at 19:27

5 Answers 5

30

I'm afraid you cannot alter the output name from within the LaTeX source: the \jobname primitive can be read but not altered. You can arrange two-file solutions which allow one LaTeX file to 'call' another, but I am not sure that will answer your problem here.

3
  • 12
    Just found this question and I have one more remark: obviously, you can do \def\jobname{whatever}, and if you do it early enough, it will affect the names of the .aux, .toc etc. files - but not the pdf.
    – mbork
    Jul 31, 2011 at 20:40
  • 2
    Can you, please, add details to the "two-file solution"? How do I "call" another tex file from another one? Oct 22, 2017 at 8:22
  • 1
    For solutions to output multiple files, refer to a related answer.
    – Coby Viner
    Jun 6, 2018 at 19:41
28

You can specify additional parameters (like jobname) at the very beginning of your main file (even before \documentclass):

%& -job-name=newfilenameialwayswanted

It slightly depends on your compiler but it should work.

An additional comment: If you use an IDE, this may cause troubles for file opening hotkeys (the file they will try to open will not be there), in this case, you can look for an option like "output profile" (TeXnicCenter name) - you can also change the filename this way.

So there're no totally convenient ways to change your output filename from source, but it is possible.

7
  • These parameters must be at the first line(s) of the file for them to work. In case anyone else is having problems with that. Mar 11, 2020 at 16:23
  • 1
    This should be the accepted answer. Jun 17, 2020 at 2:05
  • does the % not out comment the code after? I use TexStudio and when I use % it gets immediately out commented. So I don't know how it should work
    – IH Pro
    May 8, 2022 at 12:40
  • Yes, a special kind of comment. Like #!/bin/bash is a comment on top of shell scripts.
    – masu
    May 16, 2022 at 20:12
  • This does not work for me (I think with TeX Live on Windows, using VS Code and a LaTeX extension there, so not sure where exactly the problem happens).
    – clel
    Jul 14, 2022 at 16:37
6

You can do as follows.

\documentclass[preview,border=12pt]{standalone}
\usepackage{filecontents}
\begin{filecontents*}{template.tex}
\documentclass[preview,border=12pt]{standalone}
\begin{document}
Hello World
\end{document}
\end{filecontents*}

\usepackage{pgffor,graphicx}
\foreach \outputfilename in {a,b,c}{\immediate\write18{pdflatex -jobname=\outputfilename\space template}}
\begin{document}
The files we created automatically are:

\foreach \outputfilename in {a,b,c}{\fbox{\includegraphics[scale=2]{\outputfilename}}\endgraf}
\end{document}

enter image description here

5
  • this didn't work for me as is. I compiled with pdflatex Any ideas? Thanks.
    – PatrickT
    Mar 15, 2017 at 12:50
  • 1
    @PatrickT you need to run pdflatex with the --shell-escape flag
    – Ahlqvist
    Mar 16, 2017 at 17:55
  • Got it, thanks Ahlqvist! (on texstudio, I just added this at the top of the document: % !TeX TXS-program:compile = txs:///pdflatex/[--shell-escape]).
    – PatrickT
    Mar 17, 2017 at 2:29
  • beware: if you are writing the list of filenames over several lines, end each line with a % to make sure you gobble up empty spaces. In my implementation \includegraphics was looking for a .pdf (with a space before the dot) and couldn't find it...
    – PatrickT
    Mar 18, 2017 at 9:43
  • Please excuse my ignorance, but my file doesn't run even when entering !TeX TXS-program:compile = txs:///pdflatex/[--shell-escape] in the first line. Error: ! LaTeX Error: Missing \begin{document}. Without the line, my error is ! LaTeX Error: File a' not found.`
    – Chris Peh
    Apr 19, 2020 at 23:35
4

Inspired by https://tex.stackexchange.com/a/5265/46718 by @ulrike-fischer , the following code do the job for me for me when compiling with the --shell-escape option:

\def\myjobname{Myfilename}

\ifx\conditionmacro\undefined
  \immediate\write18{%
    pdflatex --jobname="\myjobname"
    "\gdef\string\conditionmacro{1}\string\input\space\jobname"
  }%
  \expandafter\stop
\fi

(this may need improvements since I'm not sure I understand all subtleties)

0

Although not exactly answering the OP's question, and looking at the accepted answer that seems to indicate a possibility via a multi-file solution I wanted to provide exactly such a multi-file solution alternative of managing such issues. If one is using VimTeX, it is quite easy to create differently named pdf files as output from the compilation process workflow of the same .tex file.

Here is an example provided the author of that plugin.

Roughly, have an outer student.tex and professor.tex (with possibly differently defined variables/commands/macros thereby controlling what gets typeset how). Both of these files get to \input{main.tex} the same file -- all three files in the same folder.

Once student.tex and professor.tex are setup, there is little to no need to keep updating them. All work can now happen in main.tex.

On opening main.tex, in Vim, the plugin automatically asks the user to specify which the calling file is -- student.tex or professor.tex. On making the choice, and on issuing the equivalent of the compile command [\ll in VimTeX], compilation occurs and based on the choice made, the output is student.pdf or professor.pdf.

See example (provided by the author of the plugin) below:

enter image description here

% main.tex
\begin{document}
Hello World! \person
\end{document}

% professor.tex
\documentclass{minimal}
\newcommand{\person}{from professor}
\input{main.tex}

% student.tex
\documentclass{minimal}
\newcommand{\person}{from student}
\input{main.tex}

You must log in to answer this question.

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