Automatically capitalize first letter of first word in a new sentence in LaTeX

memius picture memius · May 12, 2010 · Viewed 13k times · Source

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?

Answer

Alexey Malistov picture Alexey Malistov · May 12, 2010

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.