emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Exam LaTeX class
@ 2021-03-18 13:46 Xianwen Chen (陈贤文)
  2021-03-19 10:13 ` Christine Köhn
  0 siblings, 1 reply; 5+ messages in thread
From: Xianwen Chen (陈贤文) @ 2021-03-18 13:46 UTC (permalink / raw)
  To: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 1358 bytes --]

Does someone have experiences with the exam LaTeX class: 
http://www-math.mit.edu/~psh/exam/examdoc.pdf?

I made a simple hack to make it work, by adding the following lines to 
the .emacs file:

(add-to-list 'org-latex-classes
'("exam"
"\\documentclass{exam}"
("\\section{%s}" . "\\section*{%s}")
("\\subsection{%s}" . "\\subsection*{%s}")
("\\subsubsection{%s}" . "\\subsubsection*{%s}")
("\\paragraph{%s}" . "\\paragraph*{%s}")
("\\subparagraph{%s}" . "\\subparagraph*{%s}")))

The next step I'm trying to do, but don't know how, is to ask LaTeX 
exporter to create two exports to PDF.

One export is the usual export.

The other export is to have [answers] in \documentclass, which in LaTeX 
will be:

\documentclass[answers]{exam}

When answers is specified, the resulted PDF will include the solutions 
that are preset in the solution environments.

The switch of printing or hiding solution environments can also be 
achieved by one of the following preamble codes: \printanswers or 
\noprintanswers.

I guess one way is to modify the org-latex-export-to-pdf function, so 
that when the document class is exam, the exporter first export without 
solutions, and then export to an other PDF file (such as 
foo-with_solutions.pdf).

But maybe someone else has hacked on the exam document class as well and 
would like to share their experiences?

Xianwen

[-- Attachment #2: Type: text/html, Size: 1706 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Exam LaTeX class
  2021-03-18 13:46 Exam LaTeX class Xianwen Chen (陈贤文)
@ 2021-03-19 10:13 ` Christine Köhn
  2021-03-24 10:26   ` Xianwen Chen (陈贤文)
  0 siblings, 1 reply; 5+ messages in thread
From: Christine Köhn @ 2021-03-19 10:13 UTC (permalink / raw)
  To: Xianwen Chen (陈贤文); +Cc: emacs-orgmode

Hi Xianwen,

Xianwen Chen (陈贤文) writes:

> Does someone have experiences with the exam LaTeX class: 
> http://www-math.mit.edu/~psh/exam/examdoc.pdf?

Yes, but I haven't useid it with orgmode.

> The next step I'm trying to do, but don't know how, is to ask LaTeX 
> exporter to create two exports to PDF.

> I guess one way is to modify the org-latex-export-to-pdf function, so 
> that when the document class is exam, the exporter first export without 
> solutions, and then export to an other PDF file (such as 
> foo-with_solutions.pdf).

Here is one way to do the latex part. You could pass a jobname to latex.

I have this

\IfEndWith*{\jobname}{withsolution}{%
  \usepackage{todonotes}
  \printanswers
}{\usepackage[disable]{todonotes}}

in a myexam.sty file to switch between modes (with or without solutions
and todo notes) and use it in the latex file with

\usepackage{myexam}

You could add your own latex class to org-latex-classes and add this
line there.

The jobname has to be passed to latex with something like -jobname
withsolution if you want it to be with solutions. I use a Makefile for
this purpose which calls latexmk

latexmk -pdf -pdflatex="pdflatex --interaction=errorstopmode" -use-make

and adds -jobname=$(basename $@) if asked to create a pdf ending with
withsolution.pdf. I can send you the Makefile if you're interested.

To use the jobname from within orgmode, you'll have to change
org-latex-pdf-process to use the jobname if needed. I think one way to
achieve this is to add a new export backend which is derived from latex
(see org-export-define-derived-backend) and which sets
org-latex-pdf-process accordingly (and resets it afterwards).

Hope this helps.

Best,
Christine


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Exam LaTeX class
  2021-03-19 10:13 ` Christine Köhn
@ 2021-03-24 10:26   ` Xianwen Chen (陈贤文)
  2021-03-24 13:16     ` Diego Zamboni
  0 siblings, 1 reply; 5+ messages in thread
From: Xianwen Chen (陈贤文) @ 2021-03-24 10:26 UTC (permalink / raw)
  To: Christine Köhn; +Cc: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 2457 bytes --]

Dear Christine (and CC list),

Thank you!

On 2021-03-19 10:13, Christine Köhn wrote:

> Here is one way to do the latex part. You could pass a jobname to 
> latex.
> 
> I have this
> 
> \IfEndWith*{\jobname}{withsolution}{%
> \usepackage{todonotes}
> \printanswers
> }{\usepackage[disable]{todonotes}}
> 
> in a myexam.sty file to switch between modes (with or without solutions
> and todo notes) and use it in the latex file with
> 
> \usepackage{myexam}
> 
> You could add your own latex class to org-latex-classes and add this
> line there.
> 
> The jobname has to be passed to latex with something like -jobname
> withsolution if you want it to be with solutions. I use a Makefile for
> this purpose which calls latexmk
> 
> latexmk -pdf -pdflatex="pdflatex --interaction=errorstopmode" -use-make
> 
> and adds -jobname=$(basename $@) if asked to create a pdf ending with
> withsolution.pdf. I can send you the Makefile if you're interested.

That's very interesting way to solve the problem using LaTeX. Thank you 
for sharing this. At the moment I'm leaning more towards solving it 
using emacs lisp.

> To use the jobname from within orgmode, you'll have to change
> org-latex-pdf-process to use the jobname if needed. I think one way to
> achieve this is to add a new export backend which is derived from latex
> (see org-export-define-derived-backend) and which sets
> org-latex-pdf-process accordingly (and resets it afterwards).

Thank you again. I'm thinking of a function like following. I'm using 
comments to express the programming detail that I don't know how to do 
yet.

(deffun org-latex-export-to-pdf-exam ()
   (interactive)
   # do some emacs lisp to add \printanswers to the end of org document 
header, i.e., adding a line of #+LATEX_HEADER: \printanswers
   (org-latex-export-to-pdf)
   # do some emacs lips to move the foo.pdf to foo-with_solutions.pdf
   # do some emacs lisp to add \noprintanswers to the end of org document 
header, i.e., removing the line of #+LATEX_HEADER: \printanswers and 
adding a line of #+LATEX_HEADER: \noprintanswers
   (org-latex-export-to-pdf)
   # remove the line of #+LATEX_HEADER: \noprintanswers
)

I don't know enough emacs lisp to fill in the details here for now. 
However, I think this would be a way to do it within emacs. So each time 
I call org-latex-export-to-pdf-exam, it would export two PDF files, one 
with solutions and one without.

What do you think?

Yours sincerely,
Xianwen

[-- Attachment #2: Type: text/html, Size: 5126 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Exam LaTeX class
  2021-03-24 10:26   ` Xianwen Chen (陈贤文)
@ 2021-03-24 13:16     ` Diego Zamboni
  2021-03-28 16:33       ` Xianwen Chen (陈贤文)
  0 siblings, 1 reply; 5+ messages in thread
From: Diego Zamboni @ 2021-03-24 13:16 UTC (permalink / raw)
  To: Xianwen Chen (陈贤文); +Cc: Org-mode, Christine Köhn

[-- Attachment #1: Type: text/plain, Size: 3626 bytes --]

Hi Xianwen,

I think the easiest way to conditionally include text in the preamble of
the document would be by including a file which can be empty sometimes, and
contain the appropriate text when needed. For example, you could have
something like this in your Org file:

#+include: ./printanswers.org
#+TITLE: Test
...


Then the following function will automatically export the file twice, one
with the \printanswers command inserted then rename the resulting file, and
export again without:

(defun org-latex-export-exams ()
  (interactive)
  (write-region "#+latex_header: \\printanswers" nil "printanswers.org")
  (org-latex-export-to-pdf)
  (rename-file (org-export-output-file-name ".pdf")
(org-export-output-file-name "-with_answers.pdf"))
  (write-region "" nil "printanswers.org")
  (org-latex-export-to-pdf))


You can then run M-x org-latex-export-exams to generate both files.

Hope this helps,
--Diego


On Wed, Mar 24, 2021 at 11:21 AM Xianwen Chen (陈贤文) <xianwen.chen@gmail.com>
wrote:

> Dear Christine (and CC list),
>
> Thank you!
>
>
>
> On 2021-03-19 10:13, Christine Köhn wrote:
>
> Here is one way to do the latex part. You could pass a jobname to latex.
>
> I have this
>
> \IfEndWith*{\jobname}{withsolution}{%
>   \usepackage{todonotes}
>   \printanswers
> }{\usepackage[disable]{todonotes}}
>
> in a myexam.sty file to switch between modes (with or without solutions
> and todo notes) and use it in the latex file with
>
> \usepackage{myexam}
>
> You could add your own latex class to org-latex-classes and add this
> line there.
>
> The jobname has to be passed to latex with something like -jobname
> withsolution if you want it to be with solutions. I use a Makefile for
> this purpose which calls latexmk
>
> latexmk -pdf -pdflatex="pdflatex --interaction=errorstopmode" -use-make
>
> and adds -jobname=$(basename $@) if asked to create a pdf ending with
> withsolution.pdf. I can send you the Makefile if you're interested.
>
>
> That's very interesting way to solve the problem using LaTeX. Thank you
> for sharing this. At the moment I'm leaning more towards solving it using
> emacs lisp.
>
>
> To use the jobname from within orgmode, you'll have to change
> org-latex-pdf-process to use the jobname if needed. I think one way to
> achieve this is to add a new export backend which is derived from latex
> (see org-export-define-derived-backend) and which sets
> org-latex-pdf-process accordingly (and resets it afterwards).
>
>
> Thank you again. I'm thinking of a function like following. I'm using
> comments to express the programming detail that I don't know how to do yet.
>
> (deffun org-latex-export-to-pdf-exam ()
>   (interactive)
>   # do some emacs lisp to add \printanswers to the end of org document
> header, i.e., adding a line of #+LATEX_HEADER: \printanswers
>   (org-latex-export-to-pdf)
>   # do some emacs lips to move the foo.pdf to foo-with_solutions.pdf
>   # do some emacs lisp to add \noprintanswers to the end of org document
> header, i.e., removing the line of #+LATEX_HEADER: \printanswers and adding
> a line of #+LATEX_HEADER: \noprintanswers
>   (org-latex-export-to-pdf)
>   # remove the line of #+LATEX_HEADER: \noprintanswers
> )
>
> I don't know enough emacs lisp to fill in the details here for now.
> However, I think this would be a way to do it within emacs. So each time I
> call org-latex-export-to-pdf-exam, it would export two PDF files, one with
> solutions and one without.
>
> What do you think?
>
> Yours sincerely,
> Xianwen
>

[-- Attachment #2: Type: text/html, Size: 6457 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Exam LaTeX class
  2021-03-24 13:16     ` Diego Zamboni
@ 2021-03-28 16:33       ` Xianwen Chen (陈贤文)
  0 siblings, 0 replies; 5+ messages in thread
From: Xianwen Chen (陈贤文) @ 2021-03-28 16:33 UTC (permalink / raw)
  To: Diego Zamboni; +Cc: Org-mode, Christine Köhn

[-- Attachment #1: Type: text/plain, Size: 4707 bytes --]

Dear Diego,

Gracias!

I slightly modified your solution.

I added the #+INCLUDE line to the end of #+LATEX_HEADER, because then it 
will overwrite any previous \printanswers and \noprintanswers defined in 
the HEADER.

#+TITLE:...

...

#+LATEX_HEADER: \usepackage{hyperref}
#+INCLUDE: ./latex-exam-printanswers-switch.org

I used the file name latex-exam-printanswers-switch.org, instead of 
printanswers.org, because it gives a better indication of what it does.

I used your function as it was, except that I changed the function name 
and updated the switch org file.

(defun org-latex-export-to-pdf-exam ()
   (interactive)
   (write-region "#+LATEX_HEADER: \\printanswers" nil 
"latex-exam-printanswers-switch.org")
   (org-latex-export-to-pdf)
   (rename-file (org-export-output-file-name ".pdf") 
(org-export-output-file-name "-with_answers.pdf"))
   (write-region "" nil "latex-exam-printanswers-switch.org")
   (org-latex-export-to-pdf)
)

It works very fine!

Have a nice Easter!

Yours sincerely,

Xianwen

On 2021-03-24 13:16, Diego Zamboni wrote:

> Hi Xianwen,
> 
> I think the easiest way to conditionally include text in the preamble 
> of the document would be by including a file which can be empty 
> sometimes, and contain the appropriate text when needed. For example, 
> you could have something like this in your Org file:
> 
>> #+include: ./printanswers.org [1]
>> #+TITLE: Test
>> ...
> 
> Then the following function will automatically export the file twice, 
> one with the \printanswers command inserted then rename the resulting 
> file, and export again without:
> 
>> (defun org-latex-export-exams ()
>> (interactive)
>> (write-region "#+latex_header: \\printanswers" nil "printanswers.org 
>> [1]")
>> (org-latex-export-to-pdf)
>> (rename-file (org-export-output-file-name ".pdf") 
>> (org-export-output-file-name "-with_answers.pdf"))
>> (write-region "" nil "printanswers.org [1]")
>> (org-latex-export-to-pdf))
> 
> You can then run M-x org-latex-export-exams to generate both files.
> 
> Hope this helps,
> --Diego
> 
> On Wed, Mar 24, 2021 at 11:21 AM Xianwen Chen (陈贤文) 
> <xianwen.chen@gmail.com> wrote:
> 
> Dear Christine (and CC list),
> 
> Thank you!
> 
> On 2021-03-19 10:13, Christine Köhn wrote:
> Here is one way to do the latex part. You could pass a jobname to 
> latex.
> 
> I have this
> 
> \IfEndWith*{\jobname}{withsolution}{%
> \usepackage{todonotes}
> \printanswers
> }{\usepackage[disable]{todonotes}}
> 
> in a myexam.sty file to switch between modes (with or without solutions
> and todo notes) and use it in the latex file with
> 
> \usepackage{myexam}
> 
> You could add your own latex class to org-latex-classes and add this
> line there.
> 
> The jobname has to be passed to latex with something like -jobname
> withsolution if you want it to be with solutions. I use a Makefile for
> this purpose which calls latexmk
> 
> latexmk -pdf -pdflatex="pdflatex --interaction=errorstopmode" -use-make
> 
> and adds -jobname=$(basename $@) if asked to create a pdf ending with
> withsolution.pdf. I can send you the Makefile if you're interested.
> 
> That's very interesting way to solve the problem using LaTeX. Thank you 
> for sharing this. At the moment I'm leaning more towards solving it 
> using emacs lisp.
> 
> To use the jobname from within orgmode, you'll have to change
> org-latex-pdf-process to use the jobname if needed. I think one way to
> achieve this is to add a new export backend which is derived from latex
> (see org-export-define-derived-backend) and which sets
> org-latex-pdf-process accordingly (and resets it afterwards).
> 
> Thank you again. I'm thinking of a function like following. I'm using 
> comments to express the programming detail that I don't know how to do 
> yet.
> 
> (deffun org-latex-export-to-pdf-exam ()
> (interactive)
> # do some emacs lisp to add \printanswers to the end of org document 
> header, i.e., adding a line of #+LATEX_HEADER: \printanswers
> (org-latex-export-to-pdf)
> # do some emacs lips to move the foo.pdf to foo-with_solutions.pdf
> # do some emacs lisp to add \noprintanswers to the end of org document 
> header, i.e., removing the line of #+LATEX_HEADER: \printanswers and 
> adding a line of #+LATEX_HEADER: \noprintanswers
> (org-latex-export-to-pdf)
> # remove the line of #+LATEX_HEADER: \noprintanswers
> )
> 
> I don't know enough emacs lisp to fill in the details here for now. 
> However, I think this would be a way to do it within emacs. So each 
> time I call org-latex-export-to-pdf-exam, it would export two PDF 
> files, one with solutions and one without.
> 
> What do you think?
> 
> Yours sincerely,
> Xianwen


Links:
------
[1] http://printanswers.org

[-- Attachment #2: Type: text/html, Size: 8534 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2021-03-28 16:28 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-03-18 13:46 Exam LaTeX class Xianwen Chen (陈贤文)
2021-03-19 10:13 ` Christine Köhn
2021-03-24 10:26   ` Xianwen Chen (陈贤文)
2021-03-24 13:16     ` Diego Zamboni
2021-03-28 16:33       ` Xianwen Chen (陈贤文)

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).