% Save file as: QUOTE.TEX              Source: FILESERV@SHSU.BITNET  
%
%  File:	QUOTE.TEX
%
%  Author:	Hunter Goatley
%		goathunter@WKUVX1.BITNET
%
%  Date:	August 14, 1991
%
%  Abstract:
%
%	This file defines the macros \begindoublequotes and \enddoublequotes,
%	which let TeX replace the double-quote character (") with TeX's
%	left double-quote and right double-quote.  For example:
%
%		"This is a test."     --->    ``This is a test.''
%
%	The double-quote character is still available via \dq.  (\" is still
%	treated as the umlaut accent.)
%
%	This macro makes a couple of assumptions about the double-quotes:
%
%	1.  Double-quotes are assumed to come in pairs.  When replacing
%	    double-quotes, the macro alternates between `` and ''.  The only
%	    exception to this is noted in (2) below.
%	2.  A double-quote at the beginning of a paragraph is always treated
%	    as ``.  This correctly handles the case where a quotation is
%	    continued into a second paragraph:
%
%		"This is the first paragraph.\par
%		"This is the second paragraph of the same quote."
%
%	Normal TeX spacing after `` and '' is maintained by saving and
%	restoring the \spacefactor.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  HOW IT WORKS:
%
%	The double-quote character (") is made active by \begindoublequotes.
%	The " macro keeps track of left-quote/right-quote pairs and inserts
%	the appropriate `` and '' in its place.
%
%	Each character has a \spacefactor associated with it, which specifies
%	the amount of stretch or shrink that a space following the character
%	can have.  Most characters have a factor of 1000, but some punctuation
%	marks have higher spacefactors, most notably the period, which has a
%	\spacefactor of 3000.  This means the space following a period can
%	stretch up to 3 times more than the space after a regular character,
%	accounting for the increased space at the end of sentences.
%
%	The `` and '' ligatures are assigned \spacefactor's of 0, so that the
%	\spacefactor that is applied to the next character is the same as that
%	of the character preceding the quotes.  Because " has been redefined as
%	a macro, any spaces following " are swallowed by TeX.  It was necessary
%	to have this macro re-insert any needed space so that the following
%	cases worked correctly:
%
%		"This is a test," she said. --> ``This is a test,'' she said.
%		"This is in a list"; etc.   --> ``This is in a list''; etc.
%
%	Without the added space, the first example becomes:
%
%		``This is a test,''she said.
%
%	The solution was to save the current \spacefactor before inserting a
%	right double-quote, then resetting the \spacefactor after the
%	insertion.  The net effect was that the " macro has a \spacefactor
%	of 0, which matches the way TeX treats `` and ''.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
{%					% Begin a group for which " is active
\catcode`\"=\active			% Make " an active character
\catcode`\@=11				% Make @ an active character
%
%  \begindoublequotes
%
%	This macro makes " an active character, resets the control sequence
%	\dblqu@te to L (left), and defines \dq as a replacement for ".
%
\gdef\begindoublequotes{%		% \begindoublequotes enables "
    \global\catcode`\"=\active		% Make " an active character
    \global\chardef\dq=`\"		% Double-quote char. via \dq
    \global\let\dblqu@te=L		% Always start with a left double-quote
    }					% End of macro
%
%  Define the macro that will be executed whenever " is encountered.
%
\gdef"{%
	%  If the double-quote is the first character in a new paragraph,
	%  make sure it becomes a left double-quote.  This case can be
	%  detected by checking to see if TeX is currently in vertical mode.
	%  If so, the double-quote is at the beginning of the paragraph
	%  (since " hasn't actually generated any horizontal mode tokens
	%  yet, TeX is still in vertical mode).  If the mode is vertical,
	%  set \dblqu@te equal to L.
	%
	\ifinner\else\ifvmode\let\dblqu@te=L\fi\fi
	%
	%  Now insert the appropriate left or right double-quote.
	%
	%  If \dblqu@te is L, insert a `` and set \dblqu@te to R.
	%
	\if L\dblqu@te``\global\let\dblqu@te=R%
	%
	%  Otherwise, save the current \spacefactor, insert '', set \dblqu@te
	%  to L, and reset the original \spacefactor.
	%
	\else
	   \let\xxx=\spacefactor		% Save the \spacefactor
	   ''\global\let\dblqu@te=L%		% Insert '' and reset \dblqu@te
	   \spacefactor\xxx			% Reset the \spacefactor
	\fi					% End of \if L\dblqu@te...
	}					% End of " macro
}						% End of group

\gdef\enddoublequotes{%
	\catcode`\"=12				%Set " back to other
	}
