1

Is it possible to use a different font whenever a capital/ upper case letter appears in the text.

So if I have a sentence, "This is some text.", then I would like the first T to be different font from the rest of the text.

If I have a sentence, "This is some text with an acronym in it GMT", then I would like the first T and GMT to be a different font from the rest.

Similarly, for "This is some text with a proper name in it: London", I would like the first T and the L to be a different font.

I am sharing a screenshot of the type of effect I would like. enter image description here

I need to do this for my entire document (possibly several chapters). So I am hoping to find a way to do this automatically, without having to change each letter individually.

2
  • 1
    Welcome to TeX.SE! Try $\cal{T}$his is some text.
    – Zarko
    Commented Mar 10, 2023 at 8:02
  • Thank you Zarko (both for the welcome and the reply). Just to clarify. I need to be able to do this for an entire document. I would rather not have to modify each capital letter individually. Although, I might end up doing that as a last resort. I will edit my question to make that clearer.
    – sasaak
    Commented Mar 10, 2023 at 8:55

2 Answers 2

6

When compiling with XeLaTeX you can use the \XeTeXinterchartoks mechanism.

The idea is to define a character class consisting of Latin upper case characters, which have hexadecimal character codes 0041 (A) to 005A (Z).

Then you define a transition from all other characters to this new class. The other characters have class 0 or 4095 for word boundaries. In this transition you can insert \cal with the next upper case letter as an argument. If I understand correctly, the insertion of this macro will trigger another transition if there are more upper case letters following the first, so subsequent letters will also be displayed as the argument of \cal.

MWE, largely based on XeLaTeX: How to specify a different font family for Latin text within non-Latin text? and Changing Digits Size For Main Font in Xelatex:

\documentclass{article}

\XeTeXinterchartokenstate = 1\relax
\newXeTeXintercharclass\ucletterclass
\ExplSyntaxOn
\int_step_inline:nnnn {"0041}{1}{"005A}
 { \XeTeXcharclass #1 = \ucletterclass }
\ExplSyntaxOff
\def\mycal#1{$\cal{#1}$}

\XeTeXinterchartoks 0 \ucletterclass = {\mycal}
\XeTeXinterchartoks 4095 \ucletterclass = {\mycal}

\begin{document}
This is some text.

This is some text with an acronym in it GMT

This is some text with a proper name in it: London
\end{document}

Result:

enter image description here

Note that you can switch the transition mechanism off temporarily with \XeTeXinterchartokenstate = 0\relax if you need unaltered upper case letters somewhere.

5
  • See also tex.stackexchange.com/questions/33855/… for a similar situation.
    – Marijn
    Commented Mar 10, 2023 at 22:06
  • Thank you Marijn. I should have specified in my question that I was working with pdflatex and my main font was frcursive (which is a T1 font. I tried to convert it to OT to get it to work with xelatex, but it did not convert properly). However, I have managed to make your example work with other fonts and xelatex. I do have a couple of follow-up questions. - Is it possible to do this type of thing with pdflatex? - What would I need to change in your code if I wanted to replace \cal with some other font (say phv or something else)? Is there a way to do that without fontspec?
    – sasaak
    Commented Mar 12, 2023 at 18:37
  • @sasaak with pdfLaTeX it is easy to do for accented letters and non-Latin scripts using \newunicodechar, however that approach is not possible for plain letters like A-Z here. I found another more complex approach for pdfLaTeX (actually TeX but it should work in LaTeX as well) in tex.stackexchange.com/a/147925 but I didn't try that out personally.
    – Marijn
    Commented Mar 13, 2023 at 10:36
  • 1
    For doing something else than \cal the procedure (for XeLaTeX) is basically the same, you can add any code to the interchartoks call. Swithing font is actually easier than doing something with an argument like \cal. Note that in that case you probably also want to switch it back at the opposite transition (i.e., from the A-Z class back to anything else), which would be \XeTeXinterchartoks \ucletterclass 0 = {something} and similarly with \ucletterclass 4095.
    – Marijn
    Commented Mar 13, 2023 at 10:40
  • 1
    Thanks again Marijn. I have managed to find a solution based on your post and the links you shared. I will share an example later in case it helps someone else. But I am marking your post as the answer since it showed me how I could do this.
    – sasaak
    Commented Mar 16, 2023 at 15:10
1

I am sharing my code/solution in case it helps someone else. This is based on Marijn's answer and the links in that post.

\documentclass[a4paper,11pt]{book}

\usepackage[default]{frcursive}



\XeTeXinterchartokenstate = 1\relax  
\newXeTeXintercharclass\ucletterclass  
\ExplSyntaxOn   
\int_step_inline:nnnn {"0041}{1}{"005A}
 { \XeTeXcharclass #1 = \ucletterclass }
\ExplSyntaxOff
\def\mycal#1{$\cal{#1}$}


\XeTeXinterchartoks 0 \ucletterclass = {\begingroup \fontfamily{phv}\fontseries{m}\fontshape{n}\selectfont}
\XeTeXinterchartoks \ucletterclass 0 = {\endgroup}
\XeTeXinterchartoks 4095 \ucletterclass = {\begingroup \fontfamily{phv}\fontseries{m}\fontshape{n}\selectfont}
\XeTeXinterchartoks \ucletterclass 4095= {\endgroup}


\begin{document}


This is some text.

This is some text with an acronym in it GMT

This is some text with a proper name in it: London


\end{document}


Here is the result.

enter image description here

You must log in to answer this question.

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