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...

lockstep
  • 250,273
R.U.R.
  • 283
  • 1
  • 2
  • 5

5 Answers5

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

cgnieder
  • 66,645
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}
David Carlisle
  • 757,742
  • 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?? – Peter Grill Mar 22 '13 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) – David Carlisle Mar 22 '13 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... :-) – Peter Grill Mar 22 '13 at 21:13
  • No I don't check for that command, I execute it and if it is defined it gobbles the uppercasing macro that follows. – David Carlisle Mar 22 '13 at 21:15
  • 2
    @PeterGrill You would have drawn the uppercase letters illuminated manuscript form, in TikZ. – David Carlisle Mar 22 '13 at 21:17
  • Of course, tikz would have been involved, along with some unnatural tree colors. :-) Seriously, it would have been involved as I would have used a \foreach loop. – Peter Grill Mar 22 '13 at 21:22
  • Thanks, it is black magic to me, but it seems it does the trick :-) – R.U.R. Mar 24 '13 at 16:21
  • @DavidCarlisle Is there any way to convert text to title case when base text is uppercase (or mixed case)? Your solution works for OP case, because all words are lowercase, but when they are uppercase it does not produce title case. I am wondering if only slight modification of your code do the trick or is something more complex needed to make it for for text that is uppercase. – Rafal Sep 30 '15 at 12:34
  • 1
    @Rafal \lowercase{\somemagiccommand{THE TABLE}} probably works – David Carlisle Sep 30 '15 at 13:03
  • @DavidCarlisle Thank you for idea. Yes, it works when done in plain text, but when I try to insert macro/variable instead of text itself it stays uppercase. Example: \def \mytext{THE TABLE} and then \lowercase{\somemagiccommand{\mytext}}. I tested also just \lowercase{\mytext} and it stays the same. While \MakeLowercase{\mytext} creates lowercase, \MakeLowercase{\somemagiccommand{\mytext}} throws errors on compilation. – Rafal Oct 01 '15 at 06:30
  • @Rafal that's expected:-) But the site doesn't really support asking new questions on comments in old ones, feel free to ask a new question (you can refer to this one) – David Carlisle Oct 01 '15 at 06:56
  • @DavidCarlisle I know I should ask a new question :) But was just curious if small tweak of your solution could fix it. (In the meantime I decided to make all text in those macros lowercase, use \somemagiccommand on that lowercase text to make them title case and then use \uppercase wherever I need text in uppercase: it's easier this way.) – Rafal Oct 01 '15 at 07:47
7

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

StrongBad
  • 20,495
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

Nils L
  • 9,716
  • 1
    I don't think this is what the OP asked for. – StrongBad Mar 22 '13 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 '13 at 21:31