11

I know one of LaTeX's bragging points is that it doesn't have this Microsoftish behavior. Nevertheless, it's sometimes useful.

LaTeX already adds an extra space after you type a (non-backslashed) period, so it should be possible to make it automatically capitalize the following letter as well.

Is there an obvious way to write a macro that does this, or is there a LaTeX package that does it already?

ErikE
  • 48,881
  • 23
  • 151
  • 196
memius
  • 4,045
  • 5
  • 26
  • 25

3 Answers3

7

The following code solves the problem.

\let\period.
\catcode`\.\active 
\def\uppercasesingleletter#1{\uppercase{#1}}
\def.{\period\afterassignment\periodx\let\next= }
\def \periodx{\ifcat\space\next \next\expandafter\uppercasesingleletter \else\expandafter\next\fi}

First. second.third.  relax.relax. up

\let\period. save period

\catcode\.\active make all periods to be active symbol (like macro).

\def\uppercasesingleletter#1{\uppercase{#1}} defines macro \uppercasesingleletter to make automatically capitalize the following letter.

\def.{\period\afterassignment\periodx\let\next= } writes saved period and checkes the next symbol.

\def \periodx{\ifcat\space\next \next\expandafter\uppercasesingleletter \else\expandafter\next\fi} If the next letter is a space then \uppercasesingleletter is inserted.

Alexey Malistov
  • 26,407
  • 13
  • 68
  • 88
  • that produced the following error: ! Undefined control sequence. .->\period \afterassignment \periodx \let \next = l.18 \parskip=0. 5\baselineskip \advance\parskip by 0pt plus 2pt ? – memius May 12 '10 at 13:26
  • 2
    You're missing a `\let\period=.` before the catcode, I think. This kind of token munging will work only if you're well-behaved about how you use periods: `{\em first.} second` doesn't work so well. +1 ingenious, though. – Charles Stewart May 12 '10 at 13:31
  • @Charles. Right. I Inserted it. – Alexey Malistov May 12 '10 at 13:35
  • i just can't get the line \catcode`\.\active to work. it produces 'undefined control sequence'. – memius May 12 '10 at 15:07
  • @Tom: \active is a chardef in Plain Tex for catcode 13, and is maybe not defined in Latex. Try running this using tex/pdftex, or using \catcode`\.=13 instead. – Charles Stewart May 12 '10 at 15:28
4

ages ago there was discussion of this idea on comp.text.tex, and the general conclusion was you can't do it satisfactorily. satisfactory, in my book, involves not making characters active, but i can't see how that could work at all.

personally, i would want to make space active, and have it then look at \spacefactor and \MakeUppercase the following character if the factor is 3000.

something like

\catcode\ \active % latex already has a saved space character -- \space
\def {\ifhmode% \spacefactor is invalid
% (or something) in vertical mode
\ifnum\spacefactor<3000\else% note: with space active,
% even cs-ended lines need %-termination
\expandafter\gobbleandupper\fi}%
\def\gobbleandupper#1{\def\tempa{#1}\def\tempb{ }%
\ifx\tempa\tempb% can''t indent the code, either :-(
% here, we have another space
\expandafter\gobbleandupper% try again
\else\space% insert a "real" space to soak up the
% space factor
\expandafter\MakeUppercase\fi}%

this doesn't really do the job -- there are enough loose ends to knit a fairisle jumper. for example, given that we can't rely on \everypar in latex, how do you uppercase the first letter of a paragraph?

no ... however much it hurts (which is why i avoid unnecessary key operations) we need to type latex "properly" :-(

3

I decided to solve it in the following way:

Since I always compile the LaTeX code three times before i okular the result (to get pagination and references right), I decided to build the capitalization of sentences into that process.

Thus, I now have a shell script that calls my capitalization script (written in CRM114) first, then pdflatex three times, and then okular. This way, all the stuff happens as the result of a single command.

memius
  • 4,045
  • 5
  • 26
  • 25
  • The CRM114 script uses a regexp and the linux command 'tr' to turn any lowercase letter preceded by a period (which, in turn, is not preceded by any known abbreviations) and some whitespace into an uppercase letter. – memius Aug 07 '10 at 17:04