I have a collection of documents that I want to read into a LaTeX environment. The problem is that these documents are not exactly formatted the way I would like. I would like to create an environment which for the duration of the environment changes the way LaTeX parses .
at the end of a sentence. Essentially, I would like something which looks like:
\begin{mycontent}
random content. more random content. some more random content.
\end{mycontent}
and parses it as though I had written
\begin{mycontent}
random content.\formattingcommand more random content.\formattingcommand some more random content.\formattingcommand
\end{mycontent}
I don't have control over the content of the files getting passed to me, but I am guaranteed that the files are all structured the same. I could write a Perl script to handle this, and normally I would. But this is something that needs some kind of LaTeX wrapper that others can use without invoking Perl and works seamlessly for them.
It would be nice if I could write something like the following:
\let\oldperiod{.}
\def.{\oldperiod\formattingcommand}%
But .
is not a command. I've looked in the Knuth's The TeXbook
in the section on sentences, but I don't see anything there I can immediately use.
I suppose I could do something that changes the cat code for .
for the duration of the environment, but I need the old period to show up within this new environment. I'm not really sure how to go this direction and would prefer not to. But, I'm willing to be flexible on this point (particularly if someone's got something really cool to show.)
Edit 1
On the subject of catcodes: I've never really worked with catcodes before. So I'm kind of blind going down that alley on my own right now.
Edit 2
If I follow JosephWright's suggestion, I might worry that some other package I load might now have a clash in how an active period will be treated. Is there a reason the following code has not been suggested:
\documentclass{article}
\newcommand{\formattingcommand}{DEMO}
\newenvironment{mycontent}
{%
\let\normalperiod=.%
\catcode`\.=\active
\def.{\normalperiod\formattingcommand}%
}
{}
\begin{document}
\begin{mycontent}
random content. more random content. some more random content.
\end{mycontent}
\end{document}
I ask, because it would seem to me that this version should work, but when I try it I get an error:
! Missing control sequence inserted.
<inserted text>
\inaccessible
l.11 \begin{mycontent}
?
Edit 3
Based upon everyone's feedback below, would the following be safe?
\documentclass{article}
\newcommand{\formattingcommand}{DEMO}
\let\normalperiod=.%
\chardef\periodcatcode=\catcode`\.
\catcode`\.=\active
\newenvironment{mycontent}
{%
\catcode`\.=\active%
\def.{\normalperiod\formattingcommand}%
}
{}
\catcode`\.=\periodcatcode
\begin{document}
\begin{mycontent}
random content. more random content. some more random content.
\end{mycontent}
\end{document}
By safe I mean:
\let
saves the correct version of period (period with the expected catcode): assuming I haven't done anything else wonky with catcodes earlier.- I've saved the current catcode before making it
\active
- I've made
.
have an\active
cat code to ensure that solely within the scope of my environment it has the\active
definition I'm giving it. In other words, my definition is local in scope and won't effect definitions outside of its scope. - I've restored the catcode of
.
to what it was before creating my new environment without having to second-guess what that might have been. - If other packages wish to redefined an active
.
, my tinkering is invisible to them (assuming I'm not using any code related to those packaged within my environment).
let
. (I'm not sure the syntax is quite right, though.) You should read this: tex.stackexchange.com/questions/258/… – Mohan Dec 30 '12 at 21:44\def\normalperiod{.}
; the syntax\let\oldperiod{.}
is definitely wrong and it should be\let\oldperiod=.
(the=
is optional). – egreg Dec 30 '12 at 22:11\lowercase
method is to be preferred, if you don't want to assign a global meaning to the active period, but it's personal preference. – egreg Dec 30 '12 at 23:22