Is there a better way to generate a separate solutions file in the exam class?












2














I write a lot of exams, quizzes, and homework problems for the course I teach. Whenever I write an evaluation foo.tex, I always want a separate solutions file foo-solutions.tex to be written. Here's an example of my workflow.



I start by writing the evaluation along with the solutions in a file foo-solutions.tex:



documentclass{exam}
printanswers

newcommand{checkforstudent}[1]{%
ifcsname#1endcsname%
noprintanswers%
else% ... command '#1' does not exist ...%
fi%
}


begin{document}

checkforstudent{studentmode}

begin{questions}
question What is the first sentence of emph{Moby Dick}?
begin{solution}
emph{Call me Ishmael.}
end{solution}
end{questions}

end{document}


The command checkforstudent tests whether or not the input exists a a command. If so, then the answers are switched off. The first line after begin{document} uses checkforstudent to check for a command



Compiling this file generates foo-solutions.pdf:



screen-shot of foo-solutions.pdf



I also have a file foo.tex:



newcommand{studentmode}{}input foo-solutions


This file defines the command studentmode and then inputs the entirety of foo-solutions.tex. Compiling foo.tex generates foo.pdf:



screen-shot of foo.pdf



Finally, I have these two pdfs generated at once with a bash script write-files.sh



#!/bin/bash

pdflatex foo-solutions.tex
pdflatex foo-solutions.tex

pdflatex foo
pdflatex foo


The script compiles both files twice to make sure all references are properly dealt with.



This workflow works reasonably well. However, there are some annoyances:




  1. I have to keep track of three files instead of one (foo-solutions.tex, foo.tex, and write-files.sh).

  2. When I write a new evaluation bar.tex, I copy foo-solutions.tex, foo.tex, and write-files.sh to a new directory and replace the instances of foo with bar. This really annoys me.

  3. Many of my colleagues are not comfortable with running bash scripts. So, when I share my documents with them, they have trouble generating both the evaluation and the solutions.


It would be nice if there were a solution to my problem that allowed me to generate foo-solutions.pdf and foo.pdf with just one latex file.



Is this possible?



Note. My current workflow was inspired by this answer to a similar question of mine.










share|improve this question





























    2














    I write a lot of exams, quizzes, and homework problems for the course I teach. Whenever I write an evaluation foo.tex, I always want a separate solutions file foo-solutions.tex to be written. Here's an example of my workflow.



    I start by writing the evaluation along with the solutions in a file foo-solutions.tex:



    documentclass{exam}
    printanswers

    newcommand{checkforstudent}[1]{%
    ifcsname#1endcsname%
    noprintanswers%
    else% ... command '#1' does not exist ...%
    fi%
    }


    begin{document}

    checkforstudent{studentmode}

    begin{questions}
    question What is the first sentence of emph{Moby Dick}?
    begin{solution}
    emph{Call me Ishmael.}
    end{solution}
    end{questions}

    end{document}


    The command checkforstudent tests whether or not the input exists a a command. If so, then the answers are switched off. The first line after begin{document} uses checkforstudent to check for a command



    Compiling this file generates foo-solutions.pdf:



    screen-shot of foo-solutions.pdf



    I also have a file foo.tex:



    newcommand{studentmode}{}input foo-solutions


    This file defines the command studentmode and then inputs the entirety of foo-solutions.tex. Compiling foo.tex generates foo.pdf:



    screen-shot of foo.pdf



    Finally, I have these two pdfs generated at once with a bash script write-files.sh



    #!/bin/bash

    pdflatex foo-solutions.tex
    pdflatex foo-solutions.tex

    pdflatex foo
    pdflatex foo


    The script compiles both files twice to make sure all references are properly dealt with.



    This workflow works reasonably well. However, there are some annoyances:




    1. I have to keep track of three files instead of one (foo-solutions.tex, foo.tex, and write-files.sh).

    2. When I write a new evaluation bar.tex, I copy foo-solutions.tex, foo.tex, and write-files.sh to a new directory and replace the instances of foo with bar. This really annoys me.

    3. Many of my colleagues are not comfortable with running bash scripts. So, when I share my documents with them, they have trouble generating both the evaluation and the solutions.


    It would be nice if there were a solution to my problem that allowed me to generate foo-solutions.pdf and foo.pdf with just one latex file.



    Is this possible?



    Note. My current workflow was inspired by this answer to a similar question of mine.










    share|improve this question



























      2












      2








      2


      1





      I write a lot of exams, quizzes, and homework problems for the course I teach. Whenever I write an evaluation foo.tex, I always want a separate solutions file foo-solutions.tex to be written. Here's an example of my workflow.



      I start by writing the evaluation along with the solutions in a file foo-solutions.tex:



      documentclass{exam}
      printanswers

      newcommand{checkforstudent}[1]{%
      ifcsname#1endcsname%
      noprintanswers%
      else% ... command '#1' does not exist ...%
      fi%
      }


      begin{document}

      checkforstudent{studentmode}

      begin{questions}
      question What is the first sentence of emph{Moby Dick}?
      begin{solution}
      emph{Call me Ishmael.}
      end{solution}
      end{questions}

      end{document}


      The command checkforstudent tests whether or not the input exists a a command. If so, then the answers are switched off. The first line after begin{document} uses checkforstudent to check for a command



      Compiling this file generates foo-solutions.pdf:



      screen-shot of foo-solutions.pdf



      I also have a file foo.tex:



      newcommand{studentmode}{}input foo-solutions


      This file defines the command studentmode and then inputs the entirety of foo-solutions.tex. Compiling foo.tex generates foo.pdf:



      screen-shot of foo.pdf



      Finally, I have these two pdfs generated at once with a bash script write-files.sh



      #!/bin/bash

      pdflatex foo-solutions.tex
      pdflatex foo-solutions.tex

      pdflatex foo
      pdflatex foo


      The script compiles both files twice to make sure all references are properly dealt with.



      This workflow works reasonably well. However, there are some annoyances:




      1. I have to keep track of three files instead of one (foo-solutions.tex, foo.tex, and write-files.sh).

      2. When I write a new evaluation bar.tex, I copy foo-solutions.tex, foo.tex, and write-files.sh to a new directory and replace the instances of foo with bar. This really annoys me.

      3. Many of my colleagues are not comfortable with running bash scripts. So, when I share my documents with them, they have trouble generating both the evaluation and the solutions.


      It would be nice if there were a solution to my problem that allowed me to generate foo-solutions.pdf and foo.pdf with just one latex file.



      Is this possible?



      Note. My current workflow was inspired by this answer to a similar question of mine.










      share|improve this question















      I write a lot of exams, quizzes, and homework problems for the course I teach. Whenever I write an evaluation foo.tex, I always want a separate solutions file foo-solutions.tex to be written. Here's an example of my workflow.



      I start by writing the evaluation along with the solutions in a file foo-solutions.tex:



      documentclass{exam}
      printanswers

      newcommand{checkforstudent}[1]{%
      ifcsname#1endcsname%
      noprintanswers%
      else% ... command '#1' does not exist ...%
      fi%
      }


      begin{document}

      checkforstudent{studentmode}

      begin{questions}
      question What is the first sentence of emph{Moby Dick}?
      begin{solution}
      emph{Call me Ishmael.}
      end{solution}
      end{questions}

      end{document}


      The command checkforstudent tests whether or not the input exists a a command. If so, then the answers are switched off. The first line after begin{document} uses checkforstudent to check for a command



      Compiling this file generates foo-solutions.pdf:



      screen-shot of foo-solutions.pdf



      I also have a file foo.tex:



      newcommand{studentmode}{}input foo-solutions


      This file defines the command studentmode and then inputs the entirety of foo-solutions.tex. Compiling foo.tex generates foo.pdf:



      screen-shot of foo.pdf



      Finally, I have these two pdfs generated at once with a bash script write-files.sh



      #!/bin/bash

      pdflatex foo-solutions.tex
      pdflatex foo-solutions.tex

      pdflatex foo
      pdflatex foo


      The script compiles both files twice to make sure all references are properly dealt with.



      This workflow works reasonably well. However, there are some annoyances:




      1. I have to keep track of three files instead of one (foo-solutions.tex, foo.tex, and write-files.sh).

      2. When I write a new evaluation bar.tex, I copy foo-solutions.tex, foo.tex, and write-files.sh to a new directory and replace the instances of foo with bar. This really annoys me.

      3. Many of my colleagues are not comfortable with running bash scripts. So, when I share my documents with them, they have trouble generating both the evaluation and the solutions.


      It would be nice if there were a solution to my problem that allowed me to generate foo-solutions.pdf and foo.pdf with just one latex file.



      Is this possible?



      Note. My current workflow was inspired by this answer to a similar question of mine.







      exam






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 9 '17 at 10:38









      arauzo

      7416




      7416










      asked Dec 7 '17 at 18:14









      Brian Fitzpatrick

      357114




      357114






















          4 Answers
          4






          active

          oldest

          votes


















          3














          Along with your first file (you called it foo-solutions.tex, but for this Makefile you should call it foo.tex) you can use the following Makefile:



          TEX   = pdflatex
          # if you want to use specific FLAGS for compilation
          FLAGS =

          FILE = $(wildcard *.tex)

          STDVER = $(FILE:%.tex=%-students)
          SOLVER = $(FILE:%.tex=%-solutions)
          STDOPT = studentmode

          STDPDF = $(STDVER).pdf
          SOLPDF = $(SOLVER).pdf

          all: $(STDPDF) $(SOLPDF)

          $(STDPDF): $(FILE)
          $(TEX) $(FLAGS) -jobname $(STDVER) "def$(STDOPT){}input{$(FILE)}"
          $(TEX) $(FLAGS) -jobname $(STDVER) "def$(STDOPT){}input{$(FILE)}"
          $(TEX) $(FLAGS) -jobname $(STDVER) "def$(STDOPT){}input{$(FILE)}"
          $(SOLPDF): $(FILE)
          $(TEX) $(FLAGS) -jobname $(SOLVER) $(FILE)
          $(TEX) $(FLAGS) -jobname $(SOLVER) $(FILE)
          $(TEX) $(FLAGS) -jobname $(SOLVER) $(FILE)

          .PHONY: clean clean_sol clean_std
          clean: clean_sol clean_std
          clean_sol:
          -rm -f $(SOLVER).*
          clean_std:
          -rm -f $(STDVER).*


          Put it in the same directory as your .tex-file called Makefile. In this directory should only be one .tex-file. If there are more change the line FILE = $(wildcard *.tex) to FILE = <your-tex-files-name>.tex. It produces two separate pdfs one called <your-tex-files-name>-students.pdf and one called <your-tex-files-name>-solutions.pdf. Call it from the shell with make.



          This is a rather basic Makefile. It could use some enhancements, but should do the trick.



          If you want to remove the created pdfs and auxiliary files, call make clean from the shell.






          share|improve this answer































            3














            1) Using the bash script you can easily avoid having two .tex files and simplify your code.



            This can be your foo.tex:



            begin{document}

            begin{questions}
            question What is the first sentence of emph{Moby Dick}?
            begin{solution}
            emph{Call me Ishmael.}
            end{solution}
            end{questions}

            end{document}


            and the script to generate foo-exam.pdf and foo-solutions.pdf:



            #!/bin/bash

            pdflatex -jobname foo-exam "documentclass{exam}input foo"
            pdflatex -jobname foo-exam "documentclass{exam}input foo"

            pdflatex -jobname foo-solutions "documentclass{exam}printanswersinput foo"
            pdflatex -jobname foo-solutions "documentclass{exam}printanswersinput foo"


            2) You can also use just one script for all evaluations, by calling the script with a parameter, like create-exam.sh foo.



            #!/bin/bash

            if [ "$1" != "" ] && [ -f "${1}".tex ]; then

            pdflatex -jobname ${1}-exam "documentclass{exam}input ${1}"
            pdflatex -jobname ${1}-exam "documentclass{exam}input ${1}"

            pdflatex -jobname ${1}-solutions "documentclass{exam}printanswersinput ${1}"
            pdflatex -jobname ${1}-solutions "documentclass{exam}printanswersinput ${1}"

            else
            echo "Syntax is:"
            echo " create-exam name (where name.tex is the file to compile)"
            fi


            3) With the parameter, if you install the script to your colleagues in their PATH once, it will work for all evaluations.



            There may be a solution without scripts by using immediatewrite18{shell command} but it would require activating this unsafe option in Latex and escaping characters in that command, which is very difficult. If avoiding scripts is an absolute requirement, I would go for the Makefile solution.






            share|improve this answer































              1














              Another option could be to ask on each TeX run whether to include the answers or not. This way you only need the TeX file and nothing else. It might get annoying to have to answer the question though.



              documentclass{exam}
              defTestIncludeAnswersTrue{true}
              defTestIncludeAnswersFalse{false}
              defaskanswers{
              typein[IncludeAnswers]{Include answers?
              (TestIncludeAnswersTrue/TestIncludeAnswersFalse)}
              ifxTestIncludeAnswersTrueIncludeAnswers
              printanswers
              else
              ifxTestIncludeAnswersFalseIncludeAnswers
              else
              typeout{Please answer with 'TestIncludeAnswersTrue' or
              'TestIncludeAnswersFalse'}
              askanswers
              fi
              fi
              }
              askanswers


              begin{document}

              begin{questions}
              question What is the first sentence of emph{Moby Dick}?
              begin{solution}
              emph{Call me Ishmael.}
              end{solution}
              end{questions}

              end{document}





              share|improve this answer





















              • When running with overleal.com, this program fails with "Emergency stop. <read 0> l.18 askanswers""
                – pzorba75
                Dec 8 '17 at 3:31












              • @pzorba75 perhaps overleaf doesn't support typein.
                – Skillmon
                Dec 8 '17 at 11:22



















              0














              The solutions posted here work well, but I think I've finally figured out an elegant solution using symlinks and latexmk.



              The package currfile can detect the name of the file being compiled by latex. The package xstring has a function IfSubStr that executes code conditioned on if a given string is a substring of another given string.



              So, if we insert the line



              IfSubStr*{currfilename}{solutions}{ printanswers }{ noprintanswers }


              in our preamble, the exam class will print solutions if the file name contains the substring solutions.



              Consider the solutions document ~/myexam/exam-solutions.tex



              documentclass{exam}

              usepackage{currfile}
              usepackage{xstring}

              IfSubStr*{currfilename}{solutions}{ printanswers }{ noprintanswers }

              begin{document}

              begin{questions}

              question What is the first sentence of emph{Moby Dick}?
              begin{solution}
              emph{Call me Ishmael.}
              end{solution}

              end{questions}

              end{document}


              Issuing ln -s ~/myexam/exam-solutions.tex ~/myexam/exam.tex creates the symlink exam.tex linked to exam-solutions.tex. Running latexmk from ~/myexam/, automatically generates both the bare exam and the solutions.






              share|improve this answer





















                Your Answer








                StackExchange.ready(function() {
                var channelOptions = {
                tags: "".split(" "),
                id: "85"
                };
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function() {
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled) {
                StackExchange.using("snippets", function() {
                createEditor();
                });
                }
                else {
                createEditor();
                }
                });

                function createEditor() {
                StackExchange.prepareEditor({
                heartbeatType: 'answer',
                autoActivateHeartbeat: false,
                convertImagesToLinks: false,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: null,
                bindNavPrevention: true,
                postfix: "",
                imageUploader: {
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                },
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                });


                }
                });














                draft saved

                draft discarded


















                StackExchange.ready(
                function () {
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f405077%2fis-there-a-better-way-to-generate-a-separate-solutions-file-in-the-exam-class%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                4 Answers
                4






                active

                oldest

                votes








                4 Answers
                4






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                3














                Along with your first file (you called it foo-solutions.tex, but for this Makefile you should call it foo.tex) you can use the following Makefile:



                TEX   = pdflatex
                # if you want to use specific FLAGS for compilation
                FLAGS =

                FILE = $(wildcard *.tex)

                STDVER = $(FILE:%.tex=%-students)
                SOLVER = $(FILE:%.tex=%-solutions)
                STDOPT = studentmode

                STDPDF = $(STDVER).pdf
                SOLPDF = $(SOLVER).pdf

                all: $(STDPDF) $(SOLPDF)

                $(STDPDF): $(FILE)
                $(TEX) $(FLAGS) -jobname $(STDVER) "def$(STDOPT){}input{$(FILE)}"
                $(TEX) $(FLAGS) -jobname $(STDVER) "def$(STDOPT){}input{$(FILE)}"
                $(TEX) $(FLAGS) -jobname $(STDVER) "def$(STDOPT){}input{$(FILE)}"
                $(SOLPDF): $(FILE)
                $(TEX) $(FLAGS) -jobname $(SOLVER) $(FILE)
                $(TEX) $(FLAGS) -jobname $(SOLVER) $(FILE)
                $(TEX) $(FLAGS) -jobname $(SOLVER) $(FILE)

                .PHONY: clean clean_sol clean_std
                clean: clean_sol clean_std
                clean_sol:
                -rm -f $(SOLVER).*
                clean_std:
                -rm -f $(STDVER).*


                Put it in the same directory as your .tex-file called Makefile. In this directory should only be one .tex-file. If there are more change the line FILE = $(wildcard *.tex) to FILE = <your-tex-files-name>.tex. It produces two separate pdfs one called <your-tex-files-name>-students.pdf and one called <your-tex-files-name>-solutions.pdf. Call it from the shell with make.



                This is a rather basic Makefile. It could use some enhancements, but should do the trick.



                If you want to remove the created pdfs and auxiliary files, call make clean from the shell.






                share|improve this answer




























                  3














                  Along with your first file (you called it foo-solutions.tex, but for this Makefile you should call it foo.tex) you can use the following Makefile:



                  TEX   = pdflatex
                  # if you want to use specific FLAGS for compilation
                  FLAGS =

                  FILE = $(wildcard *.tex)

                  STDVER = $(FILE:%.tex=%-students)
                  SOLVER = $(FILE:%.tex=%-solutions)
                  STDOPT = studentmode

                  STDPDF = $(STDVER).pdf
                  SOLPDF = $(SOLVER).pdf

                  all: $(STDPDF) $(SOLPDF)

                  $(STDPDF): $(FILE)
                  $(TEX) $(FLAGS) -jobname $(STDVER) "def$(STDOPT){}input{$(FILE)}"
                  $(TEX) $(FLAGS) -jobname $(STDVER) "def$(STDOPT){}input{$(FILE)}"
                  $(TEX) $(FLAGS) -jobname $(STDVER) "def$(STDOPT){}input{$(FILE)}"
                  $(SOLPDF): $(FILE)
                  $(TEX) $(FLAGS) -jobname $(SOLVER) $(FILE)
                  $(TEX) $(FLAGS) -jobname $(SOLVER) $(FILE)
                  $(TEX) $(FLAGS) -jobname $(SOLVER) $(FILE)

                  .PHONY: clean clean_sol clean_std
                  clean: clean_sol clean_std
                  clean_sol:
                  -rm -f $(SOLVER).*
                  clean_std:
                  -rm -f $(STDVER).*


                  Put it in the same directory as your .tex-file called Makefile. In this directory should only be one .tex-file. If there are more change the line FILE = $(wildcard *.tex) to FILE = <your-tex-files-name>.tex. It produces two separate pdfs one called <your-tex-files-name>-students.pdf and one called <your-tex-files-name>-solutions.pdf. Call it from the shell with make.



                  This is a rather basic Makefile. It could use some enhancements, but should do the trick.



                  If you want to remove the created pdfs and auxiliary files, call make clean from the shell.






                  share|improve this answer


























                    3












                    3








                    3






                    Along with your first file (you called it foo-solutions.tex, but for this Makefile you should call it foo.tex) you can use the following Makefile:



                    TEX   = pdflatex
                    # if you want to use specific FLAGS for compilation
                    FLAGS =

                    FILE = $(wildcard *.tex)

                    STDVER = $(FILE:%.tex=%-students)
                    SOLVER = $(FILE:%.tex=%-solutions)
                    STDOPT = studentmode

                    STDPDF = $(STDVER).pdf
                    SOLPDF = $(SOLVER).pdf

                    all: $(STDPDF) $(SOLPDF)

                    $(STDPDF): $(FILE)
                    $(TEX) $(FLAGS) -jobname $(STDVER) "def$(STDOPT){}input{$(FILE)}"
                    $(TEX) $(FLAGS) -jobname $(STDVER) "def$(STDOPT){}input{$(FILE)}"
                    $(TEX) $(FLAGS) -jobname $(STDVER) "def$(STDOPT){}input{$(FILE)}"
                    $(SOLPDF): $(FILE)
                    $(TEX) $(FLAGS) -jobname $(SOLVER) $(FILE)
                    $(TEX) $(FLAGS) -jobname $(SOLVER) $(FILE)
                    $(TEX) $(FLAGS) -jobname $(SOLVER) $(FILE)

                    .PHONY: clean clean_sol clean_std
                    clean: clean_sol clean_std
                    clean_sol:
                    -rm -f $(SOLVER).*
                    clean_std:
                    -rm -f $(STDVER).*


                    Put it in the same directory as your .tex-file called Makefile. In this directory should only be one .tex-file. If there are more change the line FILE = $(wildcard *.tex) to FILE = <your-tex-files-name>.tex. It produces two separate pdfs one called <your-tex-files-name>-students.pdf and one called <your-tex-files-name>-solutions.pdf. Call it from the shell with make.



                    This is a rather basic Makefile. It could use some enhancements, but should do the trick.



                    If you want to remove the created pdfs and auxiliary files, call make clean from the shell.






                    share|improve this answer














                    Along with your first file (you called it foo-solutions.tex, but for this Makefile you should call it foo.tex) you can use the following Makefile:



                    TEX   = pdflatex
                    # if you want to use specific FLAGS for compilation
                    FLAGS =

                    FILE = $(wildcard *.tex)

                    STDVER = $(FILE:%.tex=%-students)
                    SOLVER = $(FILE:%.tex=%-solutions)
                    STDOPT = studentmode

                    STDPDF = $(STDVER).pdf
                    SOLPDF = $(SOLVER).pdf

                    all: $(STDPDF) $(SOLPDF)

                    $(STDPDF): $(FILE)
                    $(TEX) $(FLAGS) -jobname $(STDVER) "def$(STDOPT){}input{$(FILE)}"
                    $(TEX) $(FLAGS) -jobname $(STDVER) "def$(STDOPT){}input{$(FILE)}"
                    $(TEX) $(FLAGS) -jobname $(STDVER) "def$(STDOPT){}input{$(FILE)}"
                    $(SOLPDF): $(FILE)
                    $(TEX) $(FLAGS) -jobname $(SOLVER) $(FILE)
                    $(TEX) $(FLAGS) -jobname $(SOLVER) $(FILE)
                    $(TEX) $(FLAGS) -jobname $(SOLVER) $(FILE)

                    .PHONY: clean clean_sol clean_std
                    clean: clean_sol clean_std
                    clean_sol:
                    -rm -f $(SOLVER).*
                    clean_std:
                    -rm -f $(STDVER).*


                    Put it in the same directory as your .tex-file called Makefile. In this directory should only be one .tex-file. If there are more change the line FILE = $(wildcard *.tex) to FILE = <your-tex-files-name>.tex. It produces two separate pdfs one called <your-tex-files-name>-students.pdf and one called <your-tex-files-name>-solutions.pdf. Call it from the shell with make.



                    This is a rather basic Makefile. It could use some enhancements, but should do the trick.



                    If you want to remove the created pdfs and auxiliary files, call make clean from the shell.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Dec 7 '17 at 22:35

























                    answered Dec 7 '17 at 21:02









                    Skillmon

                    21k11941




                    21k11941























                        3














                        1) Using the bash script you can easily avoid having two .tex files and simplify your code.



                        This can be your foo.tex:



                        begin{document}

                        begin{questions}
                        question What is the first sentence of emph{Moby Dick}?
                        begin{solution}
                        emph{Call me Ishmael.}
                        end{solution}
                        end{questions}

                        end{document}


                        and the script to generate foo-exam.pdf and foo-solutions.pdf:



                        #!/bin/bash

                        pdflatex -jobname foo-exam "documentclass{exam}input foo"
                        pdflatex -jobname foo-exam "documentclass{exam}input foo"

                        pdflatex -jobname foo-solutions "documentclass{exam}printanswersinput foo"
                        pdflatex -jobname foo-solutions "documentclass{exam}printanswersinput foo"


                        2) You can also use just one script for all evaluations, by calling the script with a parameter, like create-exam.sh foo.



                        #!/bin/bash

                        if [ "$1" != "" ] && [ -f "${1}".tex ]; then

                        pdflatex -jobname ${1}-exam "documentclass{exam}input ${1}"
                        pdflatex -jobname ${1}-exam "documentclass{exam}input ${1}"

                        pdflatex -jobname ${1}-solutions "documentclass{exam}printanswersinput ${1}"
                        pdflatex -jobname ${1}-solutions "documentclass{exam}printanswersinput ${1}"

                        else
                        echo "Syntax is:"
                        echo " create-exam name (where name.tex is the file to compile)"
                        fi


                        3) With the parameter, if you install the script to your colleagues in their PATH once, it will work for all evaluations.



                        There may be a solution without scripts by using immediatewrite18{shell command} but it would require activating this unsafe option in Latex and escaping characters in that command, which is very difficult. If avoiding scripts is an absolute requirement, I would go for the Makefile solution.






                        share|improve this answer




























                          3














                          1) Using the bash script you can easily avoid having two .tex files and simplify your code.



                          This can be your foo.tex:



                          begin{document}

                          begin{questions}
                          question What is the first sentence of emph{Moby Dick}?
                          begin{solution}
                          emph{Call me Ishmael.}
                          end{solution}
                          end{questions}

                          end{document}


                          and the script to generate foo-exam.pdf and foo-solutions.pdf:



                          #!/bin/bash

                          pdflatex -jobname foo-exam "documentclass{exam}input foo"
                          pdflatex -jobname foo-exam "documentclass{exam}input foo"

                          pdflatex -jobname foo-solutions "documentclass{exam}printanswersinput foo"
                          pdflatex -jobname foo-solutions "documentclass{exam}printanswersinput foo"


                          2) You can also use just one script for all evaluations, by calling the script with a parameter, like create-exam.sh foo.



                          #!/bin/bash

                          if [ "$1" != "" ] && [ -f "${1}".tex ]; then

                          pdflatex -jobname ${1}-exam "documentclass{exam}input ${1}"
                          pdflatex -jobname ${1}-exam "documentclass{exam}input ${1}"

                          pdflatex -jobname ${1}-solutions "documentclass{exam}printanswersinput ${1}"
                          pdflatex -jobname ${1}-solutions "documentclass{exam}printanswersinput ${1}"

                          else
                          echo "Syntax is:"
                          echo " create-exam name (where name.tex is the file to compile)"
                          fi


                          3) With the parameter, if you install the script to your colleagues in their PATH once, it will work for all evaluations.



                          There may be a solution without scripts by using immediatewrite18{shell command} but it would require activating this unsafe option in Latex and escaping characters in that command, which is very difficult. If avoiding scripts is an absolute requirement, I would go for the Makefile solution.






                          share|improve this answer


























                            3












                            3








                            3






                            1) Using the bash script you can easily avoid having two .tex files and simplify your code.



                            This can be your foo.tex:



                            begin{document}

                            begin{questions}
                            question What is the first sentence of emph{Moby Dick}?
                            begin{solution}
                            emph{Call me Ishmael.}
                            end{solution}
                            end{questions}

                            end{document}


                            and the script to generate foo-exam.pdf and foo-solutions.pdf:



                            #!/bin/bash

                            pdflatex -jobname foo-exam "documentclass{exam}input foo"
                            pdflatex -jobname foo-exam "documentclass{exam}input foo"

                            pdflatex -jobname foo-solutions "documentclass{exam}printanswersinput foo"
                            pdflatex -jobname foo-solutions "documentclass{exam}printanswersinput foo"


                            2) You can also use just one script for all evaluations, by calling the script with a parameter, like create-exam.sh foo.



                            #!/bin/bash

                            if [ "$1" != "" ] && [ -f "${1}".tex ]; then

                            pdflatex -jobname ${1}-exam "documentclass{exam}input ${1}"
                            pdflatex -jobname ${1}-exam "documentclass{exam}input ${1}"

                            pdflatex -jobname ${1}-solutions "documentclass{exam}printanswersinput ${1}"
                            pdflatex -jobname ${1}-solutions "documentclass{exam}printanswersinput ${1}"

                            else
                            echo "Syntax is:"
                            echo " create-exam name (where name.tex is the file to compile)"
                            fi


                            3) With the parameter, if you install the script to your colleagues in their PATH once, it will work for all evaluations.



                            There may be a solution without scripts by using immediatewrite18{shell command} but it would require activating this unsafe option in Latex and escaping characters in that command, which is very difficult. If avoiding scripts is an absolute requirement, I would go for the Makefile solution.






                            share|improve this answer














                            1) Using the bash script you can easily avoid having two .tex files and simplify your code.



                            This can be your foo.tex:



                            begin{document}

                            begin{questions}
                            question What is the first sentence of emph{Moby Dick}?
                            begin{solution}
                            emph{Call me Ishmael.}
                            end{solution}
                            end{questions}

                            end{document}


                            and the script to generate foo-exam.pdf and foo-solutions.pdf:



                            #!/bin/bash

                            pdflatex -jobname foo-exam "documentclass{exam}input foo"
                            pdflatex -jobname foo-exam "documentclass{exam}input foo"

                            pdflatex -jobname foo-solutions "documentclass{exam}printanswersinput foo"
                            pdflatex -jobname foo-solutions "documentclass{exam}printanswersinput foo"


                            2) You can also use just one script for all evaluations, by calling the script with a parameter, like create-exam.sh foo.



                            #!/bin/bash

                            if [ "$1" != "" ] && [ -f "${1}".tex ]; then

                            pdflatex -jobname ${1}-exam "documentclass{exam}input ${1}"
                            pdflatex -jobname ${1}-exam "documentclass{exam}input ${1}"

                            pdflatex -jobname ${1}-solutions "documentclass{exam}printanswersinput ${1}"
                            pdflatex -jobname ${1}-solutions "documentclass{exam}printanswersinput ${1}"

                            else
                            echo "Syntax is:"
                            echo " create-exam name (where name.tex is the file to compile)"
                            fi


                            3) With the parameter, if you install the script to your colleagues in their PATH once, it will work for all evaluations.



                            There may be a solution without scripts by using immediatewrite18{shell command} but it would require activating this unsafe option in Latex and escaping characters in that command, which is very difficult. If avoiding scripts is an absolute requirement, I would go for the Makefile solution.







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Dec 8 '17 at 0:49

























                            answered Dec 7 '17 at 22:39









                            arauzo

                            7416




                            7416























                                1














                                Another option could be to ask on each TeX run whether to include the answers or not. This way you only need the TeX file and nothing else. It might get annoying to have to answer the question though.



                                documentclass{exam}
                                defTestIncludeAnswersTrue{true}
                                defTestIncludeAnswersFalse{false}
                                defaskanswers{
                                typein[IncludeAnswers]{Include answers?
                                (TestIncludeAnswersTrue/TestIncludeAnswersFalse)}
                                ifxTestIncludeAnswersTrueIncludeAnswers
                                printanswers
                                else
                                ifxTestIncludeAnswersFalseIncludeAnswers
                                else
                                typeout{Please answer with 'TestIncludeAnswersTrue' or
                                'TestIncludeAnswersFalse'}
                                askanswers
                                fi
                                fi
                                }
                                askanswers


                                begin{document}

                                begin{questions}
                                question What is the first sentence of emph{Moby Dick}?
                                begin{solution}
                                emph{Call me Ishmael.}
                                end{solution}
                                end{questions}

                                end{document}





                                share|improve this answer





















                                • When running with overleal.com, this program fails with "Emergency stop. <read 0> l.18 askanswers""
                                  – pzorba75
                                  Dec 8 '17 at 3:31












                                • @pzorba75 perhaps overleaf doesn't support typein.
                                  – Skillmon
                                  Dec 8 '17 at 11:22
















                                1














                                Another option could be to ask on each TeX run whether to include the answers or not. This way you only need the TeX file and nothing else. It might get annoying to have to answer the question though.



                                documentclass{exam}
                                defTestIncludeAnswersTrue{true}
                                defTestIncludeAnswersFalse{false}
                                defaskanswers{
                                typein[IncludeAnswers]{Include answers?
                                (TestIncludeAnswersTrue/TestIncludeAnswersFalse)}
                                ifxTestIncludeAnswersTrueIncludeAnswers
                                printanswers
                                else
                                ifxTestIncludeAnswersFalseIncludeAnswers
                                else
                                typeout{Please answer with 'TestIncludeAnswersTrue' or
                                'TestIncludeAnswersFalse'}
                                askanswers
                                fi
                                fi
                                }
                                askanswers


                                begin{document}

                                begin{questions}
                                question What is the first sentence of emph{Moby Dick}?
                                begin{solution}
                                emph{Call me Ishmael.}
                                end{solution}
                                end{questions}

                                end{document}





                                share|improve this answer





















                                • When running with overleal.com, this program fails with "Emergency stop. <read 0> l.18 askanswers""
                                  – pzorba75
                                  Dec 8 '17 at 3:31












                                • @pzorba75 perhaps overleaf doesn't support typein.
                                  – Skillmon
                                  Dec 8 '17 at 11:22














                                1












                                1








                                1






                                Another option could be to ask on each TeX run whether to include the answers or not. This way you only need the TeX file and nothing else. It might get annoying to have to answer the question though.



                                documentclass{exam}
                                defTestIncludeAnswersTrue{true}
                                defTestIncludeAnswersFalse{false}
                                defaskanswers{
                                typein[IncludeAnswers]{Include answers?
                                (TestIncludeAnswersTrue/TestIncludeAnswersFalse)}
                                ifxTestIncludeAnswersTrueIncludeAnswers
                                printanswers
                                else
                                ifxTestIncludeAnswersFalseIncludeAnswers
                                else
                                typeout{Please answer with 'TestIncludeAnswersTrue' or
                                'TestIncludeAnswersFalse'}
                                askanswers
                                fi
                                fi
                                }
                                askanswers


                                begin{document}

                                begin{questions}
                                question What is the first sentence of emph{Moby Dick}?
                                begin{solution}
                                emph{Call me Ishmael.}
                                end{solution}
                                end{questions}

                                end{document}





                                share|improve this answer












                                Another option could be to ask on each TeX run whether to include the answers or not. This way you only need the TeX file and nothing else. It might get annoying to have to answer the question though.



                                documentclass{exam}
                                defTestIncludeAnswersTrue{true}
                                defTestIncludeAnswersFalse{false}
                                defaskanswers{
                                typein[IncludeAnswers]{Include answers?
                                (TestIncludeAnswersTrue/TestIncludeAnswersFalse)}
                                ifxTestIncludeAnswersTrueIncludeAnswers
                                printanswers
                                else
                                ifxTestIncludeAnswersFalseIncludeAnswers
                                else
                                typeout{Please answer with 'TestIncludeAnswersTrue' or
                                'TestIncludeAnswersFalse'}
                                askanswers
                                fi
                                fi
                                }
                                askanswers


                                begin{document}

                                begin{questions}
                                question What is the first sentence of emph{Moby Dick}?
                                begin{solution}
                                emph{Call me Ishmael.}
                                end{solution}
                                end{questions}

                                end{document}






                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Dec 7 '17 at 23:01









                                Skillmon

                                21k11941




                                21k11941












                                • When running with overleal.com, this program fails with "Emergency stop. <read 0> l.18 askanswers""
                                  – pzorba75
                                  Dec 8 '17 at 3:31












                                • @pzorba75 perhaps overleaf doesn't support typein.
                                  – Skillmon
                                  Dec 8 '17 at 11:22


















                                • When running with overleal.com, this program fails with "Emergency stop. <read 0> l.18 askanswers""
                                  – pzorba75
                                  Dec 8 '17 at 3:31












                                • @pzorba75 perhaps overleaf doesn't support typein.
                                  – Skillmon
                                  Dec 8 '17 at 11:22
















                                When running with overleal.com, this program fails with "Emergency stop. <read 0> l.18 askanswers""
                                – pzorba75
                                Dec 8 '17 at 3:31






                                When running with overleal.com, this program fails with "Emergency stop. <read 0> l.18 askanswers""
                                – pzorba75
                                Dec 8 '17 at 3:31














                                @pzorba75 perhaps overleaf doesn't support typein.
                                – Skillmon
                                Dec 8 '17 at 11:22




                                @pzorba75 perhaps overleaf doesn't support typein.
                                – Skillmon
                                Dec 8 '17 at 11:22











                                0














                                The solutions posted here work well, but I think I've finally figured out an elegant solution using symlinks and latexmk.



                                The package currfile can detect the name of the file being compiled by latex. The package xstring has a function IfSubStr that executes code conditioned on if a given string is a substring of another given string.



                                So, if we insert the line



                                IfSubStr*{currfilename}{solutions}{ printanswers }{ noprintanswers }


                                in our preamble, the exam class will print solutions if the file name contains the substring solutions.



                                Consider the solutions document ~/myexam/exam-solutions.tex



                                documentclass{exam}

                                usepackage{currfile}
                                usepackage{xstring}

                                IfSubStr*{currfilename}{solutions}{ printanswers }{ noprintanswers }

                                begin{document}

                                begin{questions}

                                question What is the first sentence of emph{Moby Dick}?
                                begin{solution}
                                emph{Call me Ishmael.}
                                end{solution}

                                end{questions}

                                end{document}


                                Issuing ln -s ~/myexam/exam-solutions.tex ~/myexam/exam.tex creates the symlink exam.tex linked to exam-solutions.tex. Running latexmk from ~/myexam/, automatically generates both the bare exam and the solutions.






                                share|improve this answer


























                                  0














                                  The solutions posted here work well, but I think I've finally figured out an elegant solution using symlinks and latexmk.



                                  The package currfile can detect the name of the file being compiled by latex. The package xstring has a function IfSubStr that executes code conditioned on if a given string is a substring of another given string.



                                  So, if we insert the line



                                  IfSubStr*{currfilename}{solutions}{ printanswers }{ noprintanswers }


                                  in our preamble, the exam class will print solutions if the file name contains the substring solutions.



                                  Consider the solutions document ~/myexam/exam-solutions.tex



                                  documentclass{exam}

                                  usepackage{currfile}
                                  usepackage{xstring}

                                  IfSubStr*{currfilename}{solutions}{ printanswers }{ noprintanswers }

                                  begin{document}

                                  begin{questions}

                                  question What is the first sentence of emph{Moby Dick}?
                                  begin{solution}
                                  emph{Call me Ishmael.}
                                  end{solution}

                                  end{questions}

                                  end{document}


                                  Issuing ln -s ~/myexam/exam-solutions.tex ~/myexam/exam.tex creates the symlink exam.tex linked to exam-solutions.tex. Running latexmk from ~/myexam/, automatically generates both the bare exam and the solutions.






                                  share|improve this answer
























                                    0












                                    0








                                    0






                                    The solutions posted here work well, but I think I've finally figured out an elegant solution using symlinks and latexmk.



                                    The package currfile can detect the name of the file being compiled by latex. The package xstring has a function IfSubStr that executes code conditioned on if a given string is a substring of another given string.



                                    So, if we insert the line



                                    IfSubStr*{currfilename}{solutions}{ printanswers }{ noprintanswers }


                                    in our preamble, the exam class will print solutions if the file name contains the substring solutions.



                                    Consider the solutions document ~/myexam/exam-solutions.tex



                                    documentclass{exam}

                                    usepackage{currfile}
                                    usepackage{xstring}

                                    IfSubStr*{currfilename}{solutions}{ printanswers }{ noprintanswers }

                                    begin{document}

                                    begin{questions}

                                    question What is the first sentence of emph{Moby Dick}?
                                    begin{solution}
                                    emph{Call me Ishmael.}
                                    end{solution}

                                    end{questions}

                                    end{document}


                                    Issuing ln -s ~/myexam/exam-solutions.tex ~/myexam/exam.tex creates the symlink exam.tex linked to exam-solutions.tex. Running latexmk from ~/myexam/, automatically generates both the bare exam and the solutions.






                                    share|improve this answer












                                    The solutions posted here work well, but I think I've finally figured out an elegant solution using symlinks and latexmk.



                                    The package currfile can detect the name of the file being compiled by latex. The package xstring has a function IfSubStr that executes code conditioned on if a given string is a substring of another given string.



                                    So, if we insert the line



                                    IfSubStr*{currfilename}{solutions}{ printanswers }{ noprintanswers }


                                    in our preamble, the exam class will print solutions if the file name contains the substring solutions.



                                    Consider the solutions document ~/myexam/exam-solutions.tex



                                    documentclass{exam}

                                    usepackage{currfile}
                                    usepackage{xstring}

                                    IfSubStr*{currfilename}{solutions}{ printanswers }{ noprintanswers }

                                    begin{document}

                                    begin{questions}

                                    question What is the first sentence of emph{Moby Dick}?
                                    begin{solution}
                                    emph{Call me Ishmael.}
                                    end{solution}

                                    end{questions}

                                    end{document}


                                    Issuing ln -s ~/myexam/exam-solutions.tex ~/myexam/exam.tex creates the symlink exam.tex linked to exam-solutions.tex. Running latexmk from ~/myexam/, automatically generates both the bare exam and the solutions.







                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered 20 mins ago









                                    Brian Fitzpatrick

                                    357114




                                    357114






























                                        draft saved

                                        draft discarded




















































                                        Thanks for contributing an answer to TeX - LaTeX Stack Exchange!


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid



                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.


                                        To learn more, see our tips on writing great answers.





                                        Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                        Please pay close attention to the following guidance:


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid



                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.


                                        To learn more, see our tips on writing great answers.




                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function () {
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f405077%2fis-there-a-better-way-to-generate-a-separate-solutions-file-in-the-exam-class%23new-answer', 'question_page');
                                        }
                                        );

                                        Post as a guest















                                        Required, but never shown





















































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown

































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown







                                        Popular posts from this blog

                                        サソリ

                                        広島県道265号伴広島線

                                        Accessing regular linux commands in Huawei's Dopra Linux