Automatically capitalize first letter of first word in a new sentence in LaTeX
Solution 1
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.
Solution 2
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.
Solution 3
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" :-(
memius
i do web scraping and statistical analysis on big chunks of text/data. my underlying interest is neural networks and distributed computing.
Updated on June 23, 2022Comments
-
memius almost 2 years
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?
-
memius almost 14 yearsthat produced the following error: ! Undefined control sequence. .->\period \afterassignment \periodx \let \next = l.18 \parskip=0. 5\baselineskip \advance\parskip by 0pt plus 2pt ?
-
Charles Stewart almost 14 yearsYou'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. -
Alexey Malistov almost 14 years@Charles. Right. I Inserted it.
-
memius almost 14 yearsi just can't get the line \catcode`\.\active to work. it produces 'undefined control sequence'.
-
Charles Stewart almost 14 years@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.
-
memius over 13 yearsThe 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.