Text FormattingExercises and solutions

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
smr01cmh
Posts: 14
Joined: Thu May 13, 2010 1:23 am

Exercises and solutions

Post by smr01cmh »

Hi all,
I'm laying some foundations of a Mathematical document that will follow the standard format of

- examples
- exercises

and hope to have a 'solutions' section at the end. I'm having trouble getting started, as I have not understood how to write counters to files.

I've pasted the code below; the problem is that only the most recent value of '\thequestioncounter' is written to 'solutions.log'.

If anyone has any ideas on how to fix this, or suggestions on a better way of accomplishing my goals, I'd very much like to hear them.

Chris

Code: Select all

\documentclass[10pt]{article}

\newwrite\solutionswrite
\openout\solutionswrite solutions.log\relax

\newcounter{questioncounter}
\setcounter{questioncounter}{0}

\newcommand{\question}[1]{
\addtocounter{questioncounter}{1} \thequestioncounter 
\write\solutionswrite{Q\thequestioncounter. #1^^J}}

\begin{document}

\question{$x=3$} Solve the following equation for $x$
\[
	2x=6
\]

\question{$x=0,4$} Solve the following equation for $x$
\[
	x(x-4)=0
\]
\closeout\solutionswrite
%\input{solutions.log}
\end{document}
Last edited by cgnieder on Tue Oct 09, 2012 12:26 pm, edited 2 times in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

sitex
Posts: 70
Joined: Sat May 09, 2009 12:37 pm

Exercises and solutions

Post by sitex »

Perhaps you will find the thread http://www.latex-community.org/forum/vi ... f=4&t=8634 helpful.
Tom
smr01cmh
Posts: 14
Joined: Thu May 13, 2010 1:23 am

Exercises and solutions

Post by smr01cmh »

Thanks for your response, but it isn't quite what I'm looking for.

My quest took me on quite a journey, which detailed

http://ctan.org/tex-archive/macros/late ... 40a3bb9879

Eventually I found that I needed \immediate. The problem (I think) was that without \immediate LaTeX will write to the file at the end of every page. As such, if thequestioncounter changes on a page, that won't be reflected in solutions.log

I've posted my new code below

Code: Select all

\documentclass[10pt]{article}

\newwrite\answerfile
\immediate\openout\answerfile solutions.log\relax
\newcounter{questioncounter}
\setcounter{questioncounter}{0}

\newcommand{\question}[1]{
% increment and display the question counter
\addtocounter{questioncounter}{1} \thequestioncounter.
% write the answers to a file
\immediate\write\answerfile{Q\thequestioncounter. #1^^J}}


\begin{document}

\question{$x=3$} Solve the following equation for $x$
\[
	2x=6
\]

\question{$m=3$} What is the slope of the line $y=3x+4$?

\question{$x=0,4$} Solve the following equation for $x$
\[
	x(x-4)=0
\]

% close the solutions file
\immediate\closeout\answerfile

% input the solutions file- woohoo!
\IfFileExists{solutions.log}{\input{solutions.log}}{}

\end{document}
Last edited by cgnieder on Tue Oct 09, 2012 12:44 pm, edited 1 time in total.
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Exercises and solutions

Post by localghost »

Use the exam class. Useful solutions can be found via forum search.


Best regards and welcome to the board
Thorsten
Post Reply