3

I have a document that I sometimes want to print as one single document, and other times I want to print it as smaller groups of pages. I would like to automate the process. I'm using TeXmaker with LuaLaTeX on a Windows 10 machine.

I'm unclear what arguments I need to pass to the \ShellEscape command. I've read several of the other posts on this but they are mostly from about 8-9 years ago and I cannot get the code to work. I cannot tell if there has been a change to TeX (or Windows) such that the solutions in these older posts no longer work.

Here is my MWE (which doesn't work).

% !TEX TS-program = lualatex
% !TEX encoding = UTF-8 Unicode
\documentclass[a4paper,12pt]{article}
\usepackage{shellesc}  %for creating multiple output files.
\usepackage{etoolbox}  %for use with the toggles function

\newtoggle{individualpages}
\togglefalse{individualpages}

\begin{document}
Test page 1.
\iftoggle{individualpages}{%
    And this too.   %testing code just to make sure the toggle is working.  
    \ShellEscape{lualatex --jobname="file1.pdf"}
}{} 

\newpage
Test page 2.
\iftoggle{individualpages}{%
    Dont forget me!
    \ShellEscape{lualatex --jobname="file2.pdf"}
}{} 

\end{document}

Ideally I would turn the toggle to false and it would compile a .pdf using the name of my .tex document of all pages. However, when I turn the toggle to true, it would create 2 separate .pdf files named "file1.pdf" and "file2.pdf", each one page and containing there respective texts. Is this even possible?

Michael
  • 464
  • What is you compilation chain, plus arguments? – Matthias Arras Oct 25 '19 at 08:52
  • @Matthias I'm not sure I understand your question. I'm trying to change the toggle to "true" and then have it produce two separate .pdf documents, the first one saying "Test page 1. And this too." and the second saying "Test page 2. Dont forget me!". – Michael Oct 25 '19 at 09:09
  • For example, do you use pdflatex and if so what arguments do you pass to it to compile? Look into your editor-settings to find it out. One common string would be pdflatex -synctex=1 -interaction=nonstopmode -file-line-error file.tex, but could be very different, too. Usually you need to 'allow' shell escape to function when you call your compiler. So you have to pass e.g the --enable-write18 option. But that may also depend on your LaTeX distribution. For MikTex, for example see here: https://tex.stackexchange.com/questions/37489/how-can-i-enable-write-18-on-a-miktex-installation – Matthias Arras Oct 25 '19 at 10:05
  • 1
    The command should be something like \ShellEscape{lualatex --jobname="file1" \jobname}. But you will also need code which selects the content. Do you really want to get specific pages, or do you want to select some part of the content, like e.g. a section? – Ulrike Fischer Oct 28 '19 at 10:15
  • @UlrikeFischer I would like to get specific pages. The document is not set up with chapters. Where would I place the command you suggest? Somewhere in the preamble, or in the specific places where I want it to output or somewhere else entirely? – Michael Oct 29 '19 at 21:17
  • 1
    well if you want to extract pages you will probably need atbegshi, see eg https://tex.stackexchange.com/a/96258/2388. – Ulrike Fischer Oct 29 '19 at 21:26
  • What about the opposite (several documents than can be compiled individually or together)? https://tex.stackexchange.com/a/227690/11604 – Fran Nov 01 '19 at 07:59
  • @ulrikeFischer thanks for that post. I think I can make that do much of what I need, but before I do I need to figure out the write command. You have \ShellEscape{lualatex --jobname="file1" \jobname} but I'm still unsure of where to put that. I'm using TeXMaker, so I don't think I have a command line that I'm really using as such. – Michael Nov 01 '19 at 19:38

2 Answers2

2

You could (ab)use the docstrip file format to generate and build all three output files simultaneously.

% \iffalse
%<*internal>
\input docstrip.tex
\keepsilent\askforoverwritefalse
\nopreamble\nopostamble
\generate{
    \file{file1.tex}{\from{\jobname.dtx}{file1}}
    \file{file2.tex}{\from{\jobname.dtx}{file2}}
    \file{all.tex}{\from{\jobname.dtx}{all}}
}
\immediate\write18{lualatex file1.tex}
\immediate\write18{lualatex file2.tex}
\immediate\write18{lualatex all.tex}
\endbatchfile
%</internal>
%<*all|file1|file2>
\documentclass[a4paper,12pt]{article}
\begin{document}
%</all|file1|file2>
%<*all|file1>
Test page 1.
%</all|file1>
%<*file1>
And this too.
%</file1>
%<*all>
\newpage
%</all>
%<*all|file2>
Test page 2.
%</all|file2>
%<*file2>
Dont forget me!
%</file2>
%<*all|file1|file2>
\end{document}
%</all|file1|file2>
% \fi

This will both generate three separate .tex files: all of the content, content of file1, and content of file2 (the \generate{...} part), and compile those files into pdf documents using LuaLaTeX (the \immediate\write18 parts). The comments of the form %<selector> start a section that you want as a separate file, and similarly %</selector> ends the section. You will find more information on the docstrip file format from this CTAN tutorial https://ctan.org/pkg/dtxtut.

In order to call lualatex to build the generated .tex files, you will need to enable shell escape when building. From the command line one would call tex --shell-escape <filename>.dtx. I don't have access to a Windows machine so cannot help with enabling shell escape on MikTeX, but perhaps following the solutions presented in How can I enable write 18 on a MikTeX installation helps.

all.pdf:

all

file1.pdf:

file1

file2.pdf:

file2

Tuomas
  • 33
  • I appreciate the effort you put into this answer, but I'm not really able to make it work. It does create the two .dtx files, but that's not what I was really after. I was trying to have LuaLaTeX create two different .pdf files, one with each of the two pages. – Michael Nov 02 '19 at 13:26
  • I've edited my answer to hopefully be more clear. It seems your problems are with enabling shell escape (write18) on MikTeX. – Tuomas Nov 27 '19 at 11:57
0

This answer does not produce two separate files simultaneously, but nevertheless has flexibility with respect to defining what is output to two or more files. It uses the \includecomment feature of the comment package to identify what should be output to each file.

This involves:

  1. Create a new conditional, here it is \ifkeep, and define whether \ifkeep is true or false. e.g. \keeptrue.
  2. Use \ifkeep to define an if else test on the actions to be performed when \ifkeep is true or false.
  3. Place code to appear in all documents, outside a \begin{keep} \end{keep} environment.
  4. Place code to appear in a subset of documents between \begin{keep} \end{keep} depending on the value of \ifkeep

With \keeptrue the output is:

enter image description here

The result with \keepfalse is Test page 1 only. And this too and the image are omitted.

The version package has similar functionality. pdfscreen exploits this functionality to be able to output content either in an article format or for on-screen presentation as a slide.

\documentclass[a4paper,12pt]{article}
\usepackage{graphicx}
\usepackage{comment}

% Toggle "keep" to determine if output is retained or discarded
\newif\ifkeep
%\keeptrue
\keepfalse

% If \ifkeep true, retain the code in the \begin{keep} environment, otherwise discard
\ifkeep
  \includecomment{keep}
\else
  \excludecomment{keep}
\fi

\begin{document}

Test page 1.

\begin{keep}
And this too.

\centering\includegraphics[width=0.6\linewidth]{example-image-a}
\end{keep}

\end{document} 
Ross
  • 5,656
  • 2
  • 14
  • 38
  • I appreciate the effort you put into this answer, but I already have the toggle working correctly. I think the problem is the outputting to separate .pdf files. I was trying to have LuaLaTeX create two different .pdf files, one with each of the two pages. – Michael Nov 02 '19 at 13:28