# [texhax] The last character of a string

Uwe Lueck uwe.lueck at web.de
Fri Jan 29 17:11:21 CET 2010

Michael Barr <barr at math.mcgill.ca> schrieb am 29.01.2010 02:17:23:
> Yes, inside TeX.  Since it pertains only to a few macros, I could search
> them by hand.  An example is that I might get a paper to edit and it has
> \subjclass{...}.  If it ends with a period, then I want to set it as is.
> If it doesn't, I want to add one.  Some authors do the one and some do the
> other.  Another one is \subsection{...} whose parameter might end in a
> period, a question mark, or even an exclamation point.  Same issue.  A few
> authors use \subsubsection.  I am ambivalent about \section since the
> section headers are separated.  If it were just my own work, I could be
> consistent, but I am TeX editor for an online journal.

\let\OrdSubsection\subsection
%% and the like with:

\newif\ifPunct
\Punctfalse
\TestDot#1\TestEnd.\TestEnd\TestStop
\TestQuestM#1\TestEnd?\TestEnd\TestStop
\TestExclaM#1\TestEnd!\TestEnd\TestStop
\AfterPunctTest{#1\ifPunct \else .\fi}%
}
\def\TestDot#1.\TestEnd#2\TestStop{\TestPunct{#2}}
\def\TestQuestM#1?\TestEnd#2\TestStop{\TestPunct{#2}}
\def\TestExclaM#1!\TestEnd#2\TestStop{\TestPunct{#2}}
\def\TestPunct#1{\ifx\TestPunct#1\TestPunct\else\Puncttrue\fi}

HTH -- Uwe.

P.S.: with optional arguments ...

> On Thu, 28 Jan 2010, Pierre MacKay wrote:
>
> > On 01/28/2010 03:25 PM, Michael Barr wrote:
> >>  Is there a simple way to find the last character of a string?  Assume the
> >>  string is brace delimited.  What I want to do is add a period unless the
> >>  string already ends in a period, question mark, or exclamation mark, but I
> >>  don't see any way short of going through the string knocking off one
> >>  character at a time.
> >>
> > I assume that this has to be done within TeX.  Otherwise it is a function of
> > whatever editor you are using:
> >
> > It is a problem I often find, and I use the emacs "narrow-to-region" command.
> > Find whatever triggers the opening brace,
> > save the position and then (if there are no intervening paired braces) find
> > the closing brace. Narrow to region, and then search just before the closing
> > brace.  It is very fast indeed.
> >
> > The need for this ability to narrow to a specific context is one of the
> > reasons for providing specific *begin* and *end* macros in many contexts,
> > rather than insisting that the context appear as a macro parameter.  The
> > LaTeX adapters of Ibycus have insisted on the macro parameter model, but in
> > Plain Tex I stick with \GK{} and \RM{} which allow a clean "narrow-to-region"
> > operation on any passage of any size.  These macros make it possible to run
> > efficiently through a long article and check the correctness of the Greek
> > without having to scroll through all the non-Greek text.
> >
> > Pierre MacKay