自动将标题中的单词首字母大写?

自动将标题中的单词首字母大写?

有没有办法让 LaTeX 自动将章节和节中单词​​的首字母大写?

我想象有东西可以查字典,比如因为、因为、因为、在...并使其保持较小(如果它不是第一个词)。

我在互联网上找不到任何东西,但我认为它会很有用,特别是在英语标题大写字母的众多惯例之间切换时。

答案1

这个怎么样:

\documentclass{article}

\usepackage{ltxcmds} % get commands: \ltx@empty, \ltx@carzero

\catcode`\@=11 % allow @ to be apart of control sequences

\def\@aaa#1 {\@capital#1 \@bbb}  % Grap word delimited by space and capitalise first char
                                 % (used for first word)

\def\@bbb#1 {\@Capital{#1} \@bbb}% Grap word delimited by space and capitalise first char
                                 % provided its not exempt (used for subsequent words).

\def\:#1\@bbb{}                  % used so we can get a space token at the end of a control sequence,
                                 % and to gobble up all the junk between \: and \bbb inclusive

\def\@capital#1{\uppercase{#1}}  % Capitalises one character.

\def\@Capital#1{%                  Checks whether #1 is exempt and capitalise if not
  \def\@stripcolon##1\:##2{\def\@word{##1}}%
  \@stripcolon #1\:\ltx@empty%     Make sure that #1 does not have \: appended to end                
  \def\@check##1,{%
    \def\@token{##1}%
    \ifx\@token\ltx@empty%         if no more tokens in exception list
      \@capital#1%
      \let\@result\ltx@carzero%    delete everything up to and including \@nil
    \else%
      \ifx\@token\@word%           if in exception list
        #1%
        \let\@result\ltx@carzero% 
      \else%
        \let\@result\@check%       check next token in exception list.
      \fi%
    \fi%
    \@result%
  }%
  \expandafter\@check\@exempt%
}%



\newcommand{\smartuppercase}[1]{\@aaa#1\: }
\newcommand{\exempt}[1]{\def\@exempt{#1,,\@nil}}

\catcode`\@=12 % disallow @ to be apart of control sequences

\begin{document}

\exempt{the,a,of} %comma separated list of words not to capitalise - no spaces allowed

\smartuppercase{hello people of earth}

\smartuppercase{the people of the earth}

\end{document}

这是我过去两天写的东西。它似乎能完成工作。我确信如果你开始用连字符连接单词、使用引号等,它就会失败。我确信它可以进一步清理,但我把这个留给别人了。

相关内容