3

Always making my lessons, i've defined some commands to separate teacher and student stuff. The structure is the same than what is described on this post. A MWE is at the end.

At the moment, i've to compile one time, changing the name of the output, changing the file.tex to set the second version, recompile and rename this second output.

I want to make my life better by changing the pdflatex compilation command in texstudio to compile twice and changing automatically the version and the corresponding output.

I've read it's possible in the command line of Texstudio and i've read it's also possible with a bash file on this post. To be honest, i can't write/use/understand a bash so i prefer the first way.

How can i achieve :

  • Compiling my foo.tex first with the option \setversion{student} and rename it as foo_student.tex
  • Compiling second time (in the same run) my foo.tex with the option \setversion{teacher} and rename it as foo_teacher.tex

What I've done so far is to compile twice and create two pdf with different name but no differences on the contents with the following command : pdflatex -synctex=1 -interaction=nonstopmode -jobname=%_student main.tex | pdflatex -synctex=1 -interaction=nonstopmode -jobname=%_teacher main.tex

Here is a MWE for main.tex

\documentclass[11pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{etoolbox}

\newbool{studentversion}
\setbool{studentversion}{false}

\newcommand\setversion[1]{%
    \def\tempa{#1}%
    \def\tempb{student}%
    \ifx\tempa\tempb
        \setbool{studentversion}{true}%
    \else
        \def\tempb{teacher}%
        \ifx\tempa\tempb
            \setbool{studentversion}{false}%
        \else
            \errmessage{Unknown value for studentversion: #1}%
        \fi
    \fi
}

\newcommand{\ProfDifference}[2]%
{%
    \ifbool{studentversion}%
    {%%True (student version)
        #1
    }%
    {%%False (teacher version)
        #2
    }
}


\setversion{teacher}  %%Modify this line to change the version
\begin{document}

    The following text depends of the version :

    \ProfDifference{It's the student version.}{It's the teacher version.}
\end{document}

1 Answer 1

2

You can do the following:

In your main.tex change the \setversion macro to:

\newcommand*\checkversion
  {%
    \begingroup
    \def\tempa{student}%
    \expandafter
    \endgroup
    \ifx\tempa\myversion
      \setbool{studentversion}{true}%
    \else
      \begingroup
      \def\tempa{teacher}%
      \expandafter
      \endgroup
      \ifx\tempa\myversion
        \setbool{studentversion}{false}%
      \else
        \errmessage{Unknown value for studentversion: \myversion}%
      \fi
    \fi
  }
\checkversion

And change the call of pdflatex to

pdflatex -synctex=1 -interaction=nonstopmode -jobname=%_student "\def\myversion{student}\input{main.tex}" | pdflatex -synctex=1 -interaction=nonstopmode -jobname=%_teacher "\def\myversion{teacher}\input{main.tex}" 

This should do. (I don't use TeXstudio, so I won't test this, but I see no reason this shouldn't work)

3
  • You made my day ! I've just switched setbool{studentversion}{true} into \setversion{student} to make my code more readable. Thanks you !
    – Piroooh
    Commented Oct 4, 2019 at 11:06
  • Note : In TeXStudio, compiling twice works only if you've checked the advanced options. Good to know for future readers. Otherwise, it only compiles the first file.
    – Piroooh
    Commented Oct 4, 2019 at 15:58
  • 1
    @Piroooh the \checkversion macro does also incorporate the functionality of your \setversion. If you want to use \setversion instead, you might as well do \expandafter\setversion\expandafter{\myversion} in your preamble and don't define a \checkversion macro.
    – Skillmon
    Commented Oct 4, 2019 at 21:26

You must log in to answer this question.

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