Monday, September 21, 2015

DOS and UNIX line breaks

Here are my scripts 2unix and 2dos, which convert a given text file to one with guaranteed UNIX line breaks or guaranteed DOS line breaks:


#!/bin/sh
# script 2unix
# AOF 14-6-2-05
# make $1 have LF line breaks
# remove all CR:
sed -i 's/\r//' $1
echo $1 now has only LF \(UNIX\) line breaks.
echo \(To view them use cat -e $1.\)



#!/bin/sh
# script 2dos
#AOF 14-6-2-15
# make $1 have CRLF line breaks
# first remove all CR:
sed -i 's/\r//' $1
# then add CR before each LF:
sed -i 's/$/\r/' $1
echo $1 now has only CRLF \(DOS\) line breaks.
echo To view them use cat -e $1.



Thursday, September 10, 2015

LaTeX: Putting answers to selected exercises in an appendix to a book

The LaTeX package answers is designed to deal with this.  Most of the online help relates to using it with an article documentclass.    I used it like this, in a book using the memoir documentclass (which allows both A5 (option a5paper) and US Trade (option ebook) paper sizes.  The bit that caused trouble, and which is handled here, is to make sure that the solution is fully and correctly labelled with the coordinates of the exercise, including the subsection number and the sequence number in the subsection, taking account of the fact that only certain numbers in the sequence have solutions.

In preamble:

\usepackage{answers}
%Exercise:
\newtheorem{Exc}{Exercise}[subsection]
%exercise without answer:
\newenvironment{ex}{\begin{Exc}\normalfont}{\end{Exc}}
%exercise with answer in appendix:
\newenvironment{exw}{\begin{Exc}%
\Writetofile{answers}{\textbf{\thesubsection.\arabic{Exc}}}%
$\dagger$\normalfont}{\end{Exc}}
\Newassociation{sol}{Solution}{answers}
\renewcommand{\Solutionlabel}[1]{\null}% suppresses the label

At the start of the document:

\begin{document}
\Opensolutionfile{answers}[exanswers]


In the text:

1. An exercise without a solution:

\begin{ex} Show that
$f(A_1\cup A_2)= f(A_1)\cup f(A_2)$.
\end{ex}

  -- This will set the exercise with a label like

1.2.3.1 Exercise Show that ...

where 1.2.3 is the subsection number, and this is the first exercise in that subsection. The "1.2.3.1 Exercise" will be in bold, and the rest in the ambient normalfont of the document.

2. An exercise with a solution:

\begin{exw} Write out the proofs of all the
remaining identities in the Proposition.
\end{exw}
\begin{sol}
Hint: Imitate the proof given for the first one.
\end{sol}

 -- This will set the exercise with a label like

1.2.3.2 Exercise $dagger$ Write out ...

where 1.2.3 is the subsection number and this is the second exercise in it.  $dagger$ will print as a dagger, flagging the fact that there is a solution given later.  The 1.2.3.2 and the material in the sol environment will be enclosed in a Solution environment and written out to the file exanswers.tex.

Finally, at the point where answers are to appear in the appendix:

\Closesolutionfile{answers}
\input{exanswers.tex}