0

I have given (it is already typeseted) two very similar projects: A catalog with exercises and solutions and a catalog with only short solutions for the exercises. Because of the use of the package comment both projects only differ by four lines of code (see the MWE) \includecomment{exercise} vs. \excludecomment{exercise}, \includecomment{answer} vs. \excludecomment{answer} and \includecomment{shortQuestions} vs. \excludecomment{shortQuestions}. Both projects have its own main file: solutions.tex for the full catalog and shortSolutions.tex for short solutions only. Both projects sharing one directory exercises with the files for exercises, let say introduction.tex, ... , measurementTechnology.tex, see the MWE. Each exercise file, for example introduction.tex contains with the environment exercise the exercise itself, the short solution with the environment shortAnswer, the full solution with environment answer and short questions for the current topic with the environment shortQuestions, see the MWE.

My task is to create always both documents with only one tex file. So one have to compile only one tex file, let say solutions.tex to get both documents. I found some information here, but I'am doing something wrong, because I receive only one file with the full catalog.

See my MWE: (Attention! It seems there is a conflict between the environment filecontents and the comment package, because of it my code below compiles only if there aren't blank spaces or tabs in front of \begin{comment environment} and \end{comment environment}. Even LaTeX comments with % after \begin{comment environment} and \end{comment environment} aren't allowed, see below.)

\documentclass[a4paper,
               10pt,
               fleqn,
               twoside,
               onecolumn,
               openright,
               english
              ]{memoir}

\begin{filecontents}{preamble.tex} \usepackage[T1]{fontenc} \usepackage[babel = true]{microtype} \usepackage[ locale = DE, per-mode = fraction, product-symbol = \cdot, product-units = single ]{siunitx} \usepackage{ amsmath, amssymb, babel, booktabs, cancel, chemplants, comment, enumitem, graphicx, lscape, lmodern, mathtools, nicefrac, polynom, stanli, xurl } \usepackage[text]{esdiff} \usepackage[ nooldvoltagedirection, european, cuteinductors ]{circuitikz} \usepackage[ pdfstartview = FitB, bookmarksnumbered, colorlinks ]{hyperref} \title{Title for solutions} \author{Author Name} \end{filecontents} \input{preamble}

\includecomment{exercise} \includecomment{shortAnswer} \includecomment{answer} \includecomment{shortQuestions}

\begin{filecontents*}{exercises/introduction.tex} \begin{exercise} Text of the current exercise. \end{exercise}

\begin{shortAnswer} Text of the idea for the solution or the final result. \end{shortAnswer}

\begin{answer} Text of the step to step solution of the current exercise. \end{answer}

\begin{shortQuestions} \begin{enumerate} \item Fist short question to the current subject. \item Second short question to the current subject. \item\ldots \end{enumerate} \end{shortQuestions} \end{filecontents*}

\begin{filecontents*}{exercises/measurementTechnology.tex} \begin{exercise} Text of the current exercise. \end{exercise}

\begin{shortAnswer} Text of the idea for the solution or the final result. \end{shortAnswer}

\begin{answer} Text of the step to step solution of the current exercise. \end{answer}

\begin{shortQuestions} \begin{enumerate} \item Fist short question to the current subject. \item Second short question to the current subject. \item\ldots \end{enumerate} \end{shortQuestions} \end{filecontents*}

\begin{filecontents}{content.tex} \frontmatter \maketitle \thispagestyle{empty} \cleardoublepage \mainmatter \part{Part 1} \chapter{Title 1} \input{exercises/introduction} \cleardoublepage \part{Part 2} \chapter{Title 1} \input{exercises/measurementTechnology} \end{filecontents}

\begin{filecontents}{shortSolutions.tex} \documentclass[ a4paper, 10pt, fleqn, twoside, onecolumn, openright, english, ngerman ]{memoir} \input{preamble} \title{Title for short solutions} \excludecomment{exercise} \includecomment{shortAnswer} \excludecomment{answer} \excludecomment{shortQuestions} \begin{document} \input{content} \end{document} \end{filecontents}

\begin{document} \input{content} \immediate\write18{latex --shell-escape shortSolutions.tex} \end{document}

I use MikTeX with TeXnicCenter to compile with LaTeX -> DviPs -> ps2pdf (I have to use it) and following profile settings:

LaTeX:

TeXnicCenter 1

Filepath: D:\TeXnicCenter\MiKTeX\texmfs\install\miktex\bin\x64\latex.exe

Arguments: --enable-write18 -interaction=nonstopmode "%wm"

DviPs (PDF):

TeXnicCenter 2

Filepath: D:\TeXnicCenter\MiKTeX\texmfs\install\miktex\bin\x64\dvips.exe

Arguments: -P pdf -R0 "%bm.dvi"

and Ghostscript (ps2pdf):

TeXnicCenter 3

Filepath: D:\TeXnicCenter\MiKTeX\texmfs\install\miktex\bin\x64\mgs.exe

Arguments: -sPAPERSIZE=a4 -dSAFER -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -sOutputFile="%bm.pdf" -c save pop -f "%bm.ps"

For the line of code \immediate\write18{latex --shell-escape shortSolutions.tex} the logfile says runsystem(latex --shell-escape shortSolutions.tex)...executed..

What am I doing wrong?

Thank you for your help and effort in advance!

Su-47
  • 2,508
  • 2
    So you do two compiles, with some kind of flag-variations? If so: don't see how you tell Latex which version you want. Or do you expect one compile creating two pdf's ? – MS-SPO Jul 18 '23 at 11:28
  • 2
    @MS-SPO It's hard for me to be sure, because I have to keep scrolling past all the unneeded class options and packages, but I think compiling the main tex file is also supposed to trigger \immediate\write18{latex --shell-escape shortSolutions.tex} (next to last line) that isn't happening. Su-47: does the log file say anything about that line? – Teepeemm Jul 18 '23 at 12:22
  • 2
    Right. I also wonder where the flag-related environments are like \begin{exercise} ... \end{exercise}, which the comment package talks about. – MS-SPO Jul 18 '23 at 13:54
  • 1
    @Teepeemm But that would require a second file shortSolutions.tex, wouldn't it? So is the idea to have two files but only directly compile one? – cfr Jul 19 '23 at 01:34
  • This answer solves your issue? – Fran Jul 19 '23 at 07:48
  • Using filecontents seems like cheating ;-) All the old files will show up afterward. – John Kormylo Jul 19 '23 at 13:41
  • 1
    Hello @John Kormylo! I use filecontents only here on TeX.Stack Exchange, because otherwise I don't have a possibility to use different files like I do on my machine. Therefore my code on my machine doesn't contain filecontents environments. – Su-47 Jul 21 '23 at 11:50
  • Hello @Fran! No, like I wrote in my unedited question my task is ...to create both documents with only one tex file. See also my edited question. – Su-47 Jul 21 '23 at 11:56
  • Hello @Teepeemm! Thank you for your good comment! See my edited question. – Su-47 Jul 21 '23 at 12:08
  • 1
    Hello @MS-SPO! Thank you for your both clarifying comments! See my edited question! – Su-47 Jul 21 '23 at 12:11
  • Hello @cfr! Thank you for your excellent comment! You are right. See my edited question. – Su-47 Jul 21 '23 at 12:13
  • 2
    Note that allows the execution of arbitrary code via \write18 and shell escape is a potential security hole. Compile two times after some minor modification of a single main file not as bad as it could seem, except t if you really need an unattended automatic generation of documents (which is not usually the case). – Fran Jul 21 '23 at 12:52
  • Why are you using different options for shell escape for the two compiles? If your compiler needs option A, presumably it wants A in both cases. – cfr Jul 21 '23 at 23:32
  • Hello @cfr! Thank for your comment! The logfile for the line of code \immediate\write18{latex --enable-write18 shortSolutions.tex} says again runsystem(latex --enable-write18 shortSolutions.tex)...executed., but I'am receive only the full catalog as before. – Su-47 Jul 22 '23 at 14:29
  • 1
    Could you trim down your example? shortSolutions.tex only needs to have \documentclass{article} \begin{document} success \end{document}, and your main document can just have the \immediate\write18. And do you need to have --enable-write18? – Teepeemm Jul 22 '23 at 15:01
  • Hello @Teepeemm! Thank you for your comment! It was the right hint! If I hardcode the \immediate\write18 command with global paths and separate it into three parts for LaTeX, DviPs (PDF) and Ghostscript (ps2pdf) I get the desired result, see also my next comment. Now it would be great to do it without global paths. On the other hand if I think that my editor uses global paths to, then it is perhaps necessary. Any ideas? About --enable-write18 I'am not sure, because I got the project with this flags. It is probably not needed. I have to test it. – Su-47 Jul 22 '23 at 16:01
  • LaTeX: \immediate\write18{D:/TeXnicCenter/MiKTeX/texmfs/install/miktex/bin/x64/latex.exe --enable-write18 shortSolutions.tex}, 2. DviPs (PDF): \immediate\write18{D:/TeXnicCenter/MiKTeX/texmfs/install/miktex/bin/x64/dvips.exe -P pdf -R0 shortSolutions.dvi} and 3. Ghostscript (ps2pdf): \immediate\write18{D:/TeXnicCenter/MiKTeX/texmfs/install/miktex/bin/x64/mgs.exe -sPAPERSIZE=a4 -dSAFER -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -sOutputFile=shortSolutions.pdf -c save pop -f shortSolutions.ps}.
  • – Su-47 Jul 22 '23 at 16:02