18

is there a way to automatically format a text in title-case? Something like typing:

\somemagiccommand{the table, the ass and the stick}

which would yield:

The Table, the Ass and the Stick

I have searched the internet but have found nothing that would make this more efficient than doing the casing manually...

3
  • Welcome to TeX.sx! Mar 22, 2013 at 20:06
  • 1
    even a plain uppercase conversion is highly non-trivial in TeX. What you're looking for is going to be even more complex, as, in addition to those fundamental problems, TeX would have to have a way of knowing the difference between a »the« and an »Ass«, which as far as I know it doesn't. There's no such thing as an integrated word list (as hyphenation works without one). A poor man's solution would be to use an editor that, at least, can turn every first letter into uppercase. <alt> + <up> in WinEdt, for example.
    – Nils L
    Mar 22, 2013 at 20:15
  • 1
    Did you look at Capitalizing strings ignoring closed class words
    – egreg
    Mar 22, 2013 at 22:13

5 Answers 5

17

enter image description here

\documentclass{article}

\makeatletter

\def\somemagiccommand#1{%
\let\tc@w\@empty
\protected@edef\tmp{\noexpand\tc@a#1\relax}\expandafter\tc@uc@\tmp}

\def\tc@a{\futurelet\tmp\tc@aa}

\def\tc@aa{%
\ifcat a\noexpand\tmp\expandafter\tc@ab
\else\expandafter\tc@ac\fi}

\def\tc@ab#1{\edef\tc@w{\tc@w#1}\tc@a}

\def\tc@ac{%
\csname tc@@\tc@w\endcsname\expandafter\tc@uc\tc@w
\let\tc@w\@empty
\ifx\tmp\@sptoken\let\next\tc@sp
\else\ifx\tmp\relax\let\next\relax
\else\let\next\tc@nxt
\fi\fi\next}

\def\tc@sp#1{ \tc@a#1}
\def\tc@nxt#1{#1\tc@a}

\def\tc@uc#1{\uppercase{#1}}
\def\tc@uc@#1#2{\uppercase{#1#2}}

\let\tc@@the\@gobbletwo
\let\tc@@and\@gobbletwo

\makeatother

\begin{document}


\somemagiccommand{the table, the ass and the stick}

\end{document}
12
  • 8
    What kind of voodoo magic is going here? You have to be hiding the words that are not to be upper cased somewhere, but I don't see it?? Mar 22, 2013 at 20:50
  • 3
    @PeterGrill \somemagiccommand was what was asked for. (The list of non uc-words is at the end of the code just before \makeatother) Mar 22, 2013 at 21:06
  • Oh so obvious now..., Well at least where to add additional words that are not to be upper cased. So then you somehow check for the existence of tc@@<xxx> to keep <xxx> lower case. Good thing I didn't see this question earlier as I would have come up with a much more complicated solution... :-) Mar 22, 2013 at 21:13
  • 2
    @PeterGrill You would have drawn the uppercase letters illuminated manuscript form, in TikZ. Mar 22, 2013 at 21:17
  • 1
    @Rafal \lowercase{\somemagiccommand{THE TABLE}} probably works Sep 30, 2015 at 13:03
20

You might want mfirstuc and its command \capitalisewords{}. In order to keep words like “the” small we must hide it from the mechanism by hiding the space before it. This is done by using \space instead of an actual space. Alternatively you can tell the parser with \MFUnocap which words to omit:

\documentclass{article}
\usepackage{mfirstuc}

\begin{document}

\capitalisewords{the table,\space the ass\space and\space the stick}

\MFUnocap{the}\MFUnocap{and}

\capitalisewords{the table, the ass and the stick}

\end{document}

enter image description here

7

The stringstrings package has a \capitalizetitle macro that does what you want. You need to define words that are not capitalized.

4

See Headings in uppercase for information about the introduction of the titlecaps package, designed for this task.

3

for the sake of completeness, here's semi-automatic solution #3:

\documentclass{scrartcl}
\usepackage{biblatex}

\begin{document}
Well {I} Have Searched the {ENTIRE} {I}nternet but Have Found Nothing\par
\MakeSentenceCase{Well {I} Have Searched the {ENTIRE} {I}nternet but Have Found Nothing}
\end{document}

enter image description here

2
  • 1
    I don't think this is what the OP asked for.
    – StrongBad
    Mar 22, 2013 at 21:01
  • that's why I pointed out it's semi-automatic. Like the one suggested by cgnieder -- which is structurally the same thing -- there's manual work to be done to indicate where to change or not to change case. Defining word lists containing exceptions is, of course, one of the better semi-automatic solutions -- but as I understand it, the OP is looking for something that just »knows« what to capitalize, thus sparing them manual work. InDesign can do that (IIRC), as can, apparently, this thing here
    – Nils L
    Mar 22, 2013 at 21:31

Not the answer you're looking for? Browse other questions tagged .