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.

1 Answers1

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}
David Carlisle
  • 757,742
  • 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
  • @g.kov that wouldn't work:-) – David Carlisle Apr 17 '13 at 15:05
  • @DavidCarlisle : what about \footnote{\tinicommand}? Is there any way to get that working? – ClintEastwood Sep 02 '13 at 19:13
  • 1
    @ClintEastwood \makeatletter \let\old@footnotetext@footnotetext \long\def@footnotetext#1{\old@footnotetext{\spacefactor1001 #1}} \makeatother – David Carlisle Sep 02 '13 at 19:56
  • @DavidCarlisle : sorry that I have to come back to this again, but it does not seem to work with comma: And, \tinycommand. (irrespective of whether or not frenchspacing is activated. I use frenchspacing, btw.). – ClintEastwood Sep 05 '13 at 15:20
  • @ClintEastwood \sfcode\:1001 \sfcode`;1001 \sfcode`,1001 should be\sfcode`:1000 \sfcode`;1000 \sfcode`,1000 ` as those ones are not end of sentence (if that does the right thing I'll update the answer) – David Carlisle Sep 05 '13 at 15:25
  • @DavidCarlisle : I just found out that your original solution works, but that it does not work in my document when I have us \usepackage{fmtcount}. If I don't use fmtcount by deleting the line, it works. Any idea as to why? – ClintEastwood Sep 05 '13 at 16:47
  • @ClintEastwood I just looked at the source of fmtcount (which I didn't know) and see nothing, but the original code treated , as end of sentence so wasn't really correct. If you can make a MWE with and without fmtcount and edit in to your question ping me here and I'll look. – David Carlisle Sep 05 '13 at 16:51
  • Thanks @DavidCarlisle for your help. See updated question with MWEs. – ClintEastwood Sep 05 '13 at 18:02
  • @ClintEastwood I see egreg already pointed to the error. – David Carlisle Sep 05 '13 at 19:52
  • @DavidCarlisle : I have a new question for you, if you want. I updated my question above. – ClintEastwood Sep 30 '13 at 21:41
  • @ClintEastwood it's better to ask a new question as a new question rather than make substantial edits to an old one, but I updated the answer this time. – David Carlisle Sep 30 '13 at 21:55
  • @DavidCarlisle : I am currently unable to submit an MWE but I experience a strange behaviour. Sometimes the command \tinycommand gets me a capitalised letter after a dot, and sometimes not. I could not find out why nor what the difference between the two cases is. I know that without an MWE no one can look into the matter. I just wanted to report. I have no clue. – ClintEastwood Feb 23 '16 at 07:57