6

In the post Fake small caps with XeTeX/fontspec? I have found the following definitions to fake small caps

   \def\mycommand{\bgroup\obeyspaces\mycommandaux}
   \def\mycommandaux#1{\mycommandauxii #1\relax\relax\egroup}
   \def\mycommandauxii#1{%
   \ifx\relax#1\else 
   \ifcat#1\@sptoken{}  
   \expandafter\expandafter\expandafter\mycommandauxii\else
   \ifnum`#1=\uccode`#1 {\normalsize #1}\else {\footnotesize \uppercase{#1}}\fi   
   \expandafter\expandafter\expandafter\mycommandauxii\expandafter\fi\fi}

   % ...

   \mycommand{This text is set in fake small caps.}

\mycommand works properly in the main text. However, when \mycommand is used inside other commands like \title of \footnote it removes the spaces between the words.

So I wonder if there is any way to make the above command to work inside other commands or if there are other ways to achieve the same result.

In the code above \normalsize and \footnotesize can be replaced some \fontspec instructions. An advantage of the approach (or similar) is that it could work for generic fonts without small caps (I am aware of fontinst and the support for creating small caps in fontforge)

Guido
  • 30,740
  • What is the question? –  Jul 25 '12 at 02:01
  • @MarcvanDongen The question is why does the posted code not work when used within another command like \title or \footnote. – Alan Munn Jul 25 '12 at 04:18
  • 1
    It can't work: it's a verbatim-like approach as \obeyspaces alters the tokenization of spaces. You need to use an alternative token-by-token mapping that respects spaces. These are always a bit tricky! – Joseph Wright Jul 25 '12 at 06:13
  • Any suggestion on how to do the alternative token-to-token mapping? – Guido Jul 25 '12 at 07:04
  • @AlanMunn Thanks for the clarification. It was sort of clear but Guido would do himself a favour if he stated an explicit question. –  Jul 25 '12 at 11:41
  • @MarkvanDongen I have explicitly included a question, and clarified the background. – Guido Jul 26 '12 at 04:55
  • I'm not sure it should be considered an advantage that it works for generic fonts which lack small-caps. It will have all of the disadvantages of the fake small-caps supported by fontinst. (In fact, it will probably have significant disadvantages even over and above those which apply to that method.) Given that we have access to fonts with proper small-caps, it seems a shame to encourage the use of faked ones. (Just as faked slanted shapes are an extremely poor substitute for true italics.) – cfr Mar 10 '14 at 03:56

1 Answers1

9

A different approach: with \fakesc you can store the "faked" text in a control sequence for later use.

\documentclass[]{article}
\usepackage{fontspec,xparse}

\ExplSyntaxOn
\NewDocumentCommand{\fakesc}{ o m }
 {
  \guido_fakesc:n { #2 }
  \IfNoValueTF{#1}
   {
    \tl_use:N \l__guido_temp_tl
   }
   {
    \cs_set_eq:NN #1 \l__guido_temp_tl
   }
 }
\cs_new_protected:Npn \guido_fakesc:n #1
 {
  \tl_set:Nn \l__guido_text_tl { #1 }
  \tl_replace_all:Nnn \l__guido_text_tl { ~ } { \q_space }
  \tl_set:Nn \l__guido_temp_tl { \group_begin: \footnotesize }
  \tl_map_inline:Nn \l__guido_text_tl
   {
    \token_if_eq_meaning:NNTF ##1 \q_space
     {
      \tl_put_right:Nn \l__guido_temp_tl { ~ }
     }
     {
      \int_compare:nTF { \char_value_uccode:n { `##1 } = `##1 }
       {
        \tl_put_right:Nn \l__guido_temp_tl { {\normalsize ##1} }
       }
       {
        \tl_put_right:Nn \l__guido_temp_tl { \tl_upper_case:n { ##1 } }
       }
     }
   }
  \tl_put_right:Nn \l__guido_temp_tl { \group_end: }
 }
\quark_new:N \q_space
\tl_new:N \l__guido_text_tl
\tl_new:N \l__guido_temp_tl
\ExplSyntaxOff

\begin{document}
\fakesc[\mytitle]{This is the title}

\title{\mytitle}
\author{An Author}
\maketitle


\fakesc{All inside this are fake caps} and this is normal

\end{document}

But this is just a hack and real small caps should be preferred. The argument to \fakesc must consist only of letters and spaces. No fancy characters or commands.

enter image description here

egreg
  • 1,121,712
  • Who's guido (from \guido_fakes)? – einpoklum Mar 09 '14 at 20:21
  • 2
    @einpoklum Guido is the questioner; I usually take the OP's nickname for the prefix. – egreg Mar 09 '14 at 20:38
  • I have problem to use the command in \@makeschapterhead. It worked in \@makechapterhead but not in the stared version. Maybe some problem with table of contents. – Sigur Feb 06 '19 at 15:20
  • @Sigur Since this code doesn't use \@makechapterhead and I can't imagine how you're using it, it's difficult to say. Anyway, using faked small caps is the very last resort, better using a font that has the real thing. – egreg Feb 06 '19 at 15:24
  • @egreg, yes, I agree. But unfortunately the editor uses a font without small caps, so I have to fake it. I redefined the chapter head from the book class to change the font and I use \fakesc{#1} inside its definition. It worked for numbered but no for the stared one. Strange. If you have time I can try to make a mwe. – Sigur Feb 06 '19 at 15:29
  • @Sigur New question, please – egreg Feb 06 '19 at 15:35
  • @egreg, done. https://tex.stackexchange.com/q/473637/14757 – Sigur Feb 06 '19 at 16:13