3

I'm using the mfirstuc package for sentence case formatting and encountering two problems:

  1. Capitalizing Phrases with Apostrophes: I need phrases with apostrophes to be correctly capitalized, where words after the apostrophe are also capitalized. For example, jardins fabuleux d'atlas should become Jardins Fabuleux d'Atlas.
  2. Mixed-Case Sentence Capitalization: I want to capitalize the first letter of each word in a mixed-case sentence, making the rest of the letters lowercase. For instance, sOmE mIxEd cAsE sEnTeNcE should transform into Some Mixed Case Sentence. My attempt to use \ecapitalisewords with \MakeLowercase like this:
\ecapitalisewords{\MakeLowercase{sOmE mIxEd cAsE sEnTeNcE}}

results in the entire sentence being lowercase. It seems like the \ecapitalisewords command is never executed on the output of \MakeLowercase.

Here's a MWE demonstrating both issues:

\documentclass{article}
\usepackage{mfirstuc}

\gMFUnocap{d'}

\begin{document}

% Issue 1
% Desired output: Jardins Fabuleux d'Atlas
\capitalisewords{jardins fabuleux d'atlas}

% Issue 2
% Desired output: Some Mixed Case Sentence
\ecapitalisewords{\MakeLowercase{sOmE mIxEd cAsE sEnTeNcE}}

% Combined issues
% Desired output: Jardins Fabuleux d'Atlas
\capitalisewords{jArDiNs fAbUlEuX d'atLaS}

\end{document}

I'm specifically using the mfirstuc package because it provides the \gMFUnocap{⟨word⟩} command to exclude certain words from being case-changed, which I need for other parts of my document. However, if there are alternative packages that can solve these issues while also providing similar functionality to \MFUnocap{⟨word⟩}, I'm open to suggestions.

I would greatly appreciate any help in resolving these capitalization issues. Thank you in advance for your assistance.

1 Answer 1

3

You can use a regex to replace instances of d' with d\MFUwordbreak{\textquoteright}.

\documentclass{article}
\usepackage{mfirstuc}

\gMFUnocap{d}

\ExplSyntaxOn
\cs_new:Nn \mycroft_wbapostrophe:
  {
    \MFUwordbreak { \textquoteright }
  }
\cs_new:Nn \mycroft_lowerthencaps:n
  {
    \ecapitalisewords { \text_lowercase:n { #1 } }
  }
\NewDocumentCommand{\mycaps}{m}
  {
    \tl_set:Nn \l_tmpa_tl { #1 }
    \regex_replace_all:nnN { d' } { d\c{mycroft_wbapostrophe:} } \l_tmpa_tl
    \exp_args:NV \ecapitalisewords \l_tmpa_tl
  }
\NewDocumentCommand{\mylowerthencaps}{m}
  {
    \tl_set:Nn \l_tmpa_tl { #1 }
    \regex_replace_all:nnN { d' } { d\c{mycroft_wbapostrophe:} } \l_tmpa_tl
    \exp_args:NV \mycroft_lowerthencaps:n \l_tmpa_tl
  }
\ExplSyntaxOff

\begin{document}

% Issue 1
% Desired output: Jardins Fabuleux d'Atlas
\mycaps{jardins fabuleux d'atlas}

% Issue 2
% Desired output: Some Mixed Case Sentence
\mylowerthencaps{sOmE mIxEd cAsE sEnTeNcE}

% Combined issues
% Desired output: Jardins Fabuleux d'Atlas
\mylowerthencaps{jArDiNs fAbUlEuX d'atLaS}

\end{document}

text

You can define a command \addapostropherule{<letters>} to apply this more generally.

\documentclass{article}
\usepackage{mfirstuc}

\ExplSyntaxOn
\cs_new:Nn \mycroft_wbapostrophe:
  {
    \MFUwordbreak { \textquoteright }
  }
\cs_new:Nn \mycroft_lowerthencaps:n
  {
    \ecapitalisewords { \text_lowercase:n { #1 } }
  }
\tl_new:N \g_mycroft_nocaps_tl
\NewDocumentCommand{\addapostropherule}{m}
  {
    \gMFUnocap{#1}
    \tl_gput_right:Nn \g_mycroft_nocaps_tl { #1 }
  }
\NewDocumentCommand{\mycaps}{m}
  {
    \tl_set:Nn \l_tmpa_tl { #1 }
    \tl_map_inline:Nn \g_mycroft_nocaps_tl
      {
        \regex_replace_all:nnN { ##1' } { ##1\c{mycroft_wbapostrophe:} } \l_tmpa_tl
      }
    \exp_args:NV \ecapitalisewords \l_tmpa_tl
  }
\NewDocumentCommand{\mylowerthencaps}{m}
  {
    \tl_set:Nn \l_tmpa_tl { #1 }
    \tl_map_inline:Nn \g_mycroft_nocaps_tl
      {
        \regex_replace_all:nnN { ##1' } { ##1\c{mycroft_wbapostrophe:} } \l_tmpa_tl
      }
    \exp_args:NV \mycroft_lowerthencaps:n \l_tmpa_tl
  }
\ExplSyntaxOff

\addapostropherule{d}
\addapostropherule{l}
\addapostropherule{qu}
\addapostropherule{s}

\begin{document}

% Issue 1
% Desired output: Jardins Fabuleux d'Atlas
\mycaps{jardins fabuleux d'atlas}

\mycaps{la maison qu'il a construite}

% Issue 2
% Desired output: Some Mixed Case Sentence
\mylowerthencaps{sOmE mIxEd cAsE sEnTeNcE}

% Combined issues
% Desired output: Jardins Fabuleux d'Atlas
\mylowerthencaps{jArDiNs fAbUlEuX d'atLaS}

\end{document}
10
  • That's a great idea. Thanks
    – Mycroft_47
    Commented Jun 23 at 14:58
  • Can your this be extended to handle other French contractions with apostrophes, like l', qu', and s', in addition to handling d'? @mbert
    – Mycroft_47
    Commented Jun 23 at 15:11
  • 1
    @Mycroft_47 Yes, I will update the answer later. I've also found a bug, \mycaps{d'atlas} still capitalizes the first letter. Working on it
    – mbert
    Commented Jun 23 at 15:55
  • 1
    @Mycroft_47 I guess this is actually intended behavior of mfirstuc. See the example with \MFUnocap{the} on page 18 of the documentation. Is the French standard to write D'Atlas if it's the start of a sentence?
    – mbert
    Commented Jun 23 at 16:00
  • 1
    @Mycroft_47 The argument is not being expanded enough. Try something like \NewDocumentCommand{\emylowerthencaps}{m}{\exp_args:Ne\mylowerthencaps{#1}}
    – mbert
    Commented Jun 24 at 0:08

You must log in to answer this question.

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