How can I check if a header has an ending full stop?
Firstly it is always best to post a MWE. As you have not posted one I am posting a general answer, for exact formatting of the section you will need to add your own formatting commands.
You don't need a \futurelet
. TeX keeps track of special codes for letters and punctuation to place the right amount of space after a stop. We can check the value of the \spacefactor
macro and add a stop or do nothing if it is already there.
\newcommand\@addpunctuation[1]{\ifnum\spacefactor>1000\else#1\fi}
The rest of the code are minor details. Here is the MWE.
\documentclass{article}
\makeatletter
\newcommand\@addpunctuation[1]{\ifnum\spacefactor>1000\else#1\fi}
\newcommand\buildhead[1]{#1\@addpunctuation.}
\DeclareRobustCommand{\addhead}[1]{\buildhead{#1}}
\DeclareRobustCommand\Section[1]{\addhead{#1}}
\def\section{\secdef \starcmd \unstarcmd}
\def\starcmd[#1]#2{\bfseries\Large\addhead{#2}}
\newcommand\unstarcmd[1]{\bfseries\setcounter{section}{O}\renewcommand\thesection{\Alph{section} \addhead{#1}}}
\makeatother
\parindent0pt
\begin{document}
\makeatletter
\section[small title]{Testing}\\
\section{Testing}\\ % remains as is
\Section{Testing}\\
\Section{Testing.}
\end{document}
If you want the macro to work both for \frenchspacing
and \nonfrenchspacing
, you can try:
\newcommand\@addpunctuation[1]{%
\nonfrenchspacing
\ifnum\sfcode`.=1000
\ifnum\spacefactor>1000\else#1\fi
\else
\ifnum\spacefactor>1000\else#1\fi
\nonfrenchspacing
\fi
}
and test with
\frenchspacing
Test. \fbox{A test test}
\nonfrenchspacing
Test. \fbox{A test test}
You can also visit the link quoted by Philippe Goutet in the comments below.
The problem can be solved at a macro level without 'meddling' with the space factor.
\documentclass{article}
\makeatletter
\usepackage{catoptions}
\robust@def*\newsection#1{%
\begingroup
\edef\currtitle{\cpttrimspaces{#1}}%
\def\currlabel{}\def\lasttok{}%
\xifinsetFT{\detokenize{\label}}{\cptoxdetok\currtitle}{}{%
\def\reserved@a##1\label##2##3\cpt@nil{%
\def\currtitle{##1##3}%
\def\currlabel{\noexpand\label{##2}}%
}%
\expandafter\reserved@a\currtitle\cpt@nil
}%
\def\getlasttok##1{%
\expandafter\ifx\@car##1\@nil\cpt@nnil
\else
\edef\lasttok{\unexpanded{##1}}%
\expandafter\getlasttok
\fi
}%
\expandafter\getlasttok\currtitle\cpt@nnil
\s@expandarg\ifinsetTF\lasttok{\dots\ldots\textellipsis}{%
\@tempswatrue
}{%
\begingroup
\lccode`\X=133
\lowercase{\endgroup\expandafter\expandafter\expandafter
\ifstrcmpTF\expandafter\@car\lasttok\@nil X}{%
\@tempswatrue
}{%
\@tempswafalse\let\elt\relax
\def\siso@do##1{%
\xifinsetFT{\detokenize{##1}\elt}{\cptoxdetok\currtitle\elt}{}{%
\@tempswatrue\loopbreak
}%
}%
\siso@@loop{.!?:\dots\ldots\textellipsis}%
}%
}%
\def\elt{\expandcsonce\currtitle}%
\cptexpandarg{\endgroup\section}{\if@tempswa\elt\else\elt.\fi\currlabel}%
}
\makeatother
\begin{document}
\newsection{Test section.\label{sec:1}}
As seen later in Section~\ref{sec:this} \ldots.
\newsection{Test section}
\newsection{Test section.}
\newsection{Is this a test section?}
\newsection{Heh, this is test section!}
\newsection{Test section\dots}
\newsection{Test section\ldots}
\newsection{Test section\textellipsis}
\newsection{Test section\label{sec:this}\ldots}
As seen earlier in Section~\ref{sec:1} \ldots.
% I have accounted for the ASCII character 133 but you may not get it to print:
\newsection{Test section …}
\end{document}