4

This question is based on @StevenB.Segletes' patch for titlecaps found here: Reformatting a Paragraph Consisting Entirely of Capital Letters

Consider the code:

\documentclass[12pt]{article}
\usepackage{titlecaps,xpatch}
\usepackage{newpxtext} % for bold small caps
\def\explowerchar#1{%
  \ifcase\numexpr`#1-`A\relax
   a\or b\or c\or d\or e\or f\or g\or h\or i\or j\or k\or l\or m\or
   n\or o\or p\or q\or r\or s\or t\or u\or v\or w\or x\or y\or z\else
   #1\fi
}
\makeatletter
\xpatchcmd{\title@string}{\@thestring#2}{\@thestring\explowerchar{#2}}{}{}
\makeatother

\begin{document}
\thispagestyle{empty}

This is a paragraph in which the first letter of each word is lower case beginning with the second word of each sentence. Applying the algorithm, the same paragraph is produced with every word capitalized.

\vspace{15pt}

\noindent \textbf{\scshape{After Applying the Algorithm:}}

\vspace{10pt}

\titlecap{This is a paragraph in which the first letter of each word is lower case beginning with the second word of each sentence. Applying the algorithm, the same paragraph is produced with every word capitalized.}
\end{document}

which produces

enter image description here

QUESTION: How may the patched algorithm be modified in order to prevent capitalization of words such as "is," "a," "on," "the," "of," "with" that are not the first word of a sentence?

Thank you.

3 Answers 3

4

You could use the mfirstuc package and add a list of excluded words yourself or use the mfirstuc-english package which already comes with several excluded words:

\documentclass[12pt]{article}

\usepackage{mfirstuc-english}
\MFUnocap{is}

\begin{document}

\capitalisewords{This is a paragraph in which the first letter of each word is lower case beginning with the second word of each sentence. Applying the algorithm, the same paragraph is produced with every word capitalized.}

\end{document}

enter image description here

4
  • Many thanks for this answer. I noticed that when I compile without \MFUnocap{is}, the word "Is" is capitalized. Hence, I see that the aforementioned command takes care of uncapitalizing "is". May I then presume that if there were a couple of other words that were capitalized, say, "word2" and "word3", and I wanted them uncapitalized, that I would indicate that by \MFUnocap{is,word2,word3}?
    – DDS
    Commented Aug 8 at 17:43
  • @DDS Why don't you try it? Commented Aug 8 at 18:09
  • O.K... I see that it doesn't work ... I guess I will have to delineate the \MFUnocap{} command as many times as needed. Perhaps that's what you meant by "add a list." Thank you for a very nice and helpful answer.
    – DDS
    Commented Aug 8 at 18:21
  • @DDS Yes, repeat the macro for every word you would like to exclude. Commented Aug 8 at 18:22
6

No packages.

\documentclass[12pt]{article}
\usepackage{newpxtext} % for bold small caps

\linespread{1.04} % Palatino is quite high

\ExplSyntaxOn

\NewDocumentCommand{\titlecap}{m}
 {
  \dds_caps:e { \text_expand:n {#1} }
 }

\clist_const:Nn \c_dds_caps_lc_clist
 {
  a, an, and, but, for, in, of, or, no,
  nor, so, some, the, with, yet,
  is, % <--- sure?
  ís, % <--- just for example
 }
\seq_new:N \l__dds_caps_text_in_seq
\seq_new:N \l__dds_caps_text_out_seq

\cs_new_protected:Nn \dds_caps:n
 {
  \seq_set_split:Nnn \l__dds_caps_text_seq {~} {#1}
  \seq_clear:N \l__dds_caps_text_out_seq
  \seq_map_inline:Nn \l__dds_caps_text_seq
   {
    \clist_if_in:NnTF \c_dds_caps_lc_clist {##1}
     {% exception
      \seq_put_right:Nn \l__dds_caps_text_out_seq {##1}
     }
     {% capitalize
      \seq_put_right:Ne \l__dds_caps_text_out_seq { \text_titlecase_first:n {##1} }
     }
   }
   \seq_use:Nn \l__dds_caps_text_out_seq {~}
 }
\cs_generate_variant:Nn \dds_caps:n {e}

\ExplSyntaxOff

\begin{document}
\thispagestyle{empty}

This is a paragraph in which the first letter of each word 
is lower case beginning with the second word of each sentence. 
Applying the algorithm, the same paragraph is produced with 
every word capitalized.

\vspace{15pt}

\noindent \textbf{\scshape{After Applying the Algorithm:}}

\vspace{10pt}

\titlecap{
  This is a paragraph in which the first letter of each ŵord 
  ís lower case beginning with the second word of each sentence. 
  Applying the àlgorithm, the same paragraph is produced with 
  every word čapitalized.
}

\end{document}

The text is split at spaces, then each word is processed and capitalized unless it's in the list of exceptions; the processed word is put in another sequence that's delivered with a space between items.

Note that words followed by punctuation will not be recognized as an exception, which in my opinion is correct.

I added some accented characters just to show that they're treated correctly.

output

5
  • Thank your for your answer; however, when I try to compile with pdflatex, it does not terminate. Should I be compiling with something else?
    – DDS
    Commented Aug 8 at 17:37
  • @DDS Quite likely you have an outdated version of LaTeX.
    – egreg
    Commented Aug 8 at 17:47
  • The version of LaTeX I used was the very last one that TUG put on a DVD---March 2023, I believe.
    – DDS
    Commented Aug 8 at 18:07
  • +1 Maybe I need a 2024 version. Many thanks for your answer.
    – DDS
    Commented Aug 8 at 18:22
  • @egreg This could become à multi-language package...
    – projetmbc
    Commented Aug 9 at 9:58
2

This also suits with your requirement:

\documentclass{book}
\usepackage{titlecaps}%

\begin{document}

\Addlcwords{is the of that for in and a with in}%%add what ever you like

\titlecap{This is for test The}

\end{document}

enter image description here

You must log in to answer this question.

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