自动将新命令的输出大写

自动将新命令的输出大写

newcommand当在句号(或问号、感叹号等)后使用该命令时,如何创建自动将首字母大写的命令?

\newcommand{\tinycommand}{tiny command}

所需输入:

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

期望结果:

这是一些文字和一条小命令。这是一些文字。小命令。

答案1

在此处输入图片描述

\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}

如果你使用\frenchspacing那么你需要

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

因此句子的结尾会被标记,但间距的变化不会很明显。


csquotes 有自己的法语间距值,从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
}

因此大于 1005 的值是句子的结束,所以你只需要调整值

\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}

相关内容