Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 446
memius Avatar asked May 12 '10 11:05

memius


3 Answers

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.

like image 116
Alexey Malistov Avatar answered Nov 15 '22 01:11

Alexey Malistov


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" :-(

like image 20
Robin Fairbairns Avatar answered Nov 15 '22 01:11

Robin Fairbairns


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.

like image 45
memius Avatar answered Nov 15 '22 00:11

memius