xmakefirstuc 与 MakeTextLowercase 结合

xmakefirstuc 与 MakeTextLowercase 结合

我正在尝试转换一个字符串,如示例中所示,除第一个字符外,所有字符均为小写。但是,xmakefirstuc我不太喜欢。这是我的 MWE:

% arara: pdflatex
\documentclass{book}
\usepackage{xstring}
\usepackage{textcase}
\usepackage{mfirstuc}
\newcommand{\howtodothis}[1]{%
    \StrSubstitute{#1}{ }{-}[\mylong]%
    \xmakefirstuc{\MakeTextLowercase{\mylong}}\\%
    \xmakefirstuc\expandafter{\MakeTextLowercase{\mylong}}\\%
    \xmakefirstuc{\expandafter\MakeTextLowercase{\mylong}}\\%
    \xmakefirstuc{\MakeTextLowercase\expandafter{\mylong}}\\%
    \xmakefirstuc{\MakeTextLowercase{\expandafter\mylong}}\\%
    \xmakefirstuc\expandafter\relax{\MakeTextLowercase{\mylong}}\\%
    \xmakefirstuc{\expandafter\relax\MakeTextLowercase{\mylong}}\\%
    \xmakefirstuc{\MakeTextLowercase\expandafter\relax{\mylong}}\\%
    \xmakefirstuc{\MakeTextLowercase{\expandafter\relax\mylong}}\\%
    \expandafter\xmakefirstuc{\MakeTextLowercase{\mylong}}\\%
    \expandafter\xmakefirstuc\expandafter{\MakeTextLowercase{\mylong}}\\%
    \expandafter\xmakefirstuc{\expandafter\MakeTextLowercase{\mylong}}\\%
    \expandafter\xmakefirstuc{\MakeTextLowercase\expandafter{\mylong}}\\%
    \expandafter\xmakefirstuc{\MakeTextLowercase{\expandafter\mylong}}\\%
    \expandafter\xmakefirstuc\expandafter\relax{\MakeTextLowercase{\mylong}}\\%
    \expandafter\xmakefirstuc{\expandafter\relax\MakeTextLowercase{\mylong}}\\%
    \expandafter\xmakefirstuc{\MakeTextLowercase\expandafter\relax{\mylong}}\\%
    \expandafter\xmakefirstuc{\MakeTextLowercase{\expandafter\relax\mylong}}\\%
}
\begin{document}\noindent%
\howtodothis{First Letter Should Be Uppercase, All Others Lowercase.}
\end{document}

答案1

\xmakefirstuc{\MakeTextLowercase{\mylong}}

是以下的快捷方式

\expandafter\makefirstuc\expandafter{\MakeTextLowercase{\mylong}}

尝试扩展\MakeTextLowercase{\mylong}。由于\MakeTextLowercase是健壮的,因此不执行任何扩展。TeX 基元\lowercase也不会扩展,因此尝试扩展为小写然后应用\makefirstuc将不起作用。但是,如果您先扩展 ,它将起作用\mylong。如下所示:

\expandafter\makefirstuc\expandafter{\expandafter\MakeTextLowercase\expandafter{\mylong}}

或者使用\xmakefirstuc

\xmakefirstuc{\expandafter\MakeTextLowercase\expandafter{\mylong}}

这是MWE:

% arara: pdflatex
\documentclass{book}
\usepackage{xstring}
\usepackage{textcase}
\usepackage{mfirstuc}
\newcommand{\howtodothis}[1]{%
    \StrSubstitute{#1}{ }{-}[\mylong]%
    \xmakefirstuc{\expandafter\MakeTextLowercase\expandafter{\mylong}}%
}
\begin{document}\noindent%
\howtodothis{First Letter Should Be Uppercase, All Others Lowercase.}
\end{document}

结果如下:

首字母大写,其他字母小写

编辑:这实际上相当于:

\MakeTextLowercase{\MakeUppercase{F}irst-Letter-Should-Be-Uppercase,-All-Others-Lowercase.}

这看起来可能有点矛盾,因为逻辑表明应该\MakeTextLowercase抵消\MakeUppercase,但这是 TeX 处理标记的方式的结果(请参阅下面@egreg 的评论)。您可以从中看到这一点:

\lowercase{\uppercase{f}}

生成大写字母 F。

答案2

大小写转换使用 TeX 原语是不可扩展的,这就是为什么尝试“强制解决问题”在这里并不容易。Nicola 已经展示了如何使用mfirstuc解决同一问题的一个“经典”方法是将第一个标记与其余所有标记分开,例如

\documentclass{book}
\usepackage{xstring}
\usepackage{textcase}
\newcommand\howtodothis[1]{%
  \howtodothisaux#1\stop
}
\newcommand\howtodothisaux{}
\long\def\howtodothisaux#1#2\stop{%
  \MakeTextUppercase{#1}%
  \MakeTextLowercase{#2}%
}
\begin{document}\noindent%
\howtodothis{First Letter Should Be Uppercase, All Others Lowercase.}
\end{document}

使用最新的expl3,您可以使用可扩展函数执行此\text_titlecase:n操作,而无需拆分输入:

\documentclass{book}
\usepackage{expl3}
\ExplSyntaxOn
\newcommand{\howtodothis}[1]{%
  \text_titlecase:n {#1}%
}
\ExplSyntaxOff
\begin{document}\noindent%
\howtodothis{First Letter Should Be Uppercase, All Others Lowercase.}
\end{document}

相关内容