Graphics, Figures & TablesWrite multiple lines and special characters to a file

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
smr01cmh
Posts: 14
Joined: Thu May 13, 2010 1:23 am

Write multiple lines and special characters to a file

Post by smr01cmh »

Hi all,
I'd like to have a command that can write multiple lines to a .log file; in particular, I wonder if there is a way to avoid using \string to escape the \ character.

I've pasted a MWE below- any help would be greatly appreciated.

Chris

Code: Select all

\documentclass[10pt,letter]{report}

% set up the answer file
\newwrite\answerfile
\immediate\openout\answerfile solutions.log\relax

% write answer
\newcommand{\writesolution}[1]{
         \immediate\write\answerfile{#1^^J}
}

\begin{document}

\section*{Questions}
Here is a question.
\writesolution{Here is an answer.}

I'd like to write tables (and other things) to solutions.log without using $\backslash$string.
\writesolution{
\string\begin{tabular}{c|c}
    $x$ & $y$ \string\\
    $1$ & $2$
\string\end{tabular}
}

% close the solutions file
\immediate\closeout\answerfile

\section*{Answers}
\IfFileExists{solutions.log}{\input{solutions.log}}{}

\end{document}

Last edited by smr01cmh on Sun May 30, 2010 9:29 pm, edited 1 time 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.

User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Write multiple lines and special characters to a file

Post by localghost »

You don't need to reinvent the wheel [1]. See also your other request [2]. Some of those classes and packages allow to externalize solutions to problems and include them later on in the document.

[1] The TeX Catalogue Online, Topic Index - Typesetting Exam Scripts, Quizzes, Flashcards, and Questionnaires
[2] View topic: Exercises and solutions


Thorsten
smr01cmh
Posts: 14
Joined: Thu May 13, 2010 1:23 am

Re: Write multiple lines and special characters to a file

Post by smr01cmh »

Many thanks for your response. I think that the link you provided will be the most useful.
Post Reply