10

How can I create a newcommand that automictically capitalises the first letter, when the command is used after a full stop (or question mark, exclamation mark, etc.)?

\newcommand{\tinycommand}{tiny command}

The desired input:

This is some text and a \tinycommand. This is some text. \tinycommand.

The desired result:

This is some text and a tiny command. This is some text. Tiny command.

9
  • 2
    You can not use _ in command names/ – David Carlisle Apr 17 '13 at 12:14
  • Either stringstrings or titlecaps packages may give you some tools to work with. – Steven B. Segletes Apr 17 '13 at 12:16
  • @StevenB.Segletes; A search for titlecaps on CTAN gives nill results. A link to more info, please. – Johan_E Apr 17 '13 at 12:26
  • @DavidCarlisle : thanks. I edited the question accordingly. – ClintEastwood Apr 17 '13 at 12:38
  • @Johan_E The package was just uploaded last week. Perhaps it is not yet installed at CTAN. – Steven B. Segletes Apr 17 '13 at 12:51
19

enter image description here

\documentclass{article}
\newcommand{\tinycommand}{\hmm{t}iny command}

\newcommand\hmm[1]{\ifnum\ifhmode\spacefactor\else2000\fi>1000 \uppercase{#1}\else#1\fi}

\begin{document}

The desired input:

This is some text and a \tinycommand. This is some text. \tinycommand.

\end{document}

If you use \frenchspacing then you would need

\def\frenchspacing{\sfcode`\.1001 \sfcode`\?1001 \sfcode`\!1001
  \sfcode`\:1001 \sfcode`\;1001 \sfcode`\,1001 }

So that end of sentence is flagged but the change of spacing won't be noticeable.


csquotes has its own french spacing values, from csquotes.sty

\def\csq@setfrcodes{%
  \ifnum\sfcode`\A=\@m
  \else
    \csq@setazcodes
  \fi
  \sfcode`\,=1003
  \sfcode`\;=1004
  \sfcode`\:=1005
  \sfcode`\.=1006
  \sfcode`\!=1007
  \sfcode`\?=1008
}

so a value of greater than 1005 is end of sentence, so you just need to adjust the values

\documentclass{article}

\usepackage{csquotes}
\usepackage{fmtcount}
   % \def\frenchspacing{\sfcode`\.1001 \sfcode`\?1001 \sfcode`\!1001 \sfcode`\:1000 \sfcode`\;1000 \sfcode`\,1000 }  % autocapitalise auch mit frenchspacing
\frenchspacing                  % remove extra space after punctuation
\newcommand{\tinycommand}{\hmm{t}iny command}

\newcommand\hmm[1]{\ifnum\ifhmode\spacefactor\else2000\fi>1005 \uppercase{#1}\else#1\fi}

\begin{document}

The desired input:

This is some text and a \tinycommand. This is some text. \tinycommand. And, \tinycommand. 
And; \tinycommand.
\textquote{And;} \tinycommand.

\end{document}
16
  • Nice “hack”... But it doesn’t work with \frenchspacing... – Johan_E Apr 17 '13 at 12:22
  • @Johan_E It would work if you used almost frenchspacing that set the spacefactor codes to 1001 which will not make a visible difference to the space but would enable end of sentence to be triggered. – David Carlisle Apr 17 '13 at 12:25
  • @Johan_E code updated for frenchspacing case – David Carlisle Apr 17 '13 at 12:28
  • @David Carlisle: \hbox{\tinicommand.}? – g.kov Apr 17 '13 at 15:04
  • 1
    @ClintEastwood \makeatletter \let\old@footnotetext\@footnotetext \long\def\@footnotetext#1{\old@footnotetext{\spacefactor1001 #1}} \makeatother – David Carlisle Sep 2 '13 at 19:56

Not the answer you're looking for? Browse other questions tagged or ask your own question.