2

I want to split a string into two parts, the first letter in the first part and the rest in the second part. Then I want to convert the whole string to uppercase and write the second part in \scriptsize. Now, the commands work individually but if I start mixing them into one command, it no longer works.

\documentclass{article}
\usepackage{amsmath,xstring}

\newcommand{\first}[1]{\StrLeft{#1}{1}}
\newcommand{\last}[1]{\StrGobbleLeft{#1}{1}}
\newcommand{\UP}[1]{\MakeUppercase{#1}}
\newcommand{\script}[1]{\scriptsize{#1}}
\newcommand{\getsc}[1]{\MakeUppercase{\first{#1}\script{\last{#1}}}}

\begin{document}
    {\scriptsize Text}, \first{Theorem}, \last{Definition}, \UP{small case text}, {\script{SOME text}},
    %\getsc{Theorem}
\end{document}

This works and gives the output enter image description here

But the following does not end compilation

\documentclass{article}
\usepackage{amsmath,xstring}

\newcommand{\first}[1]{\StrLeft{#1}{1}}
\newcommand{\last}[1]{\StrGobbleLeft{#1}{1}}
\newcommand{\UP}[1]{\MakeUppercase{#1}}
\newcommand{\script}[1]{\scriptsize{#1}}
\newcommand{\getsc}[1]{\MakeUppercase{\first{#1}\script{\last{#1}}}}

\begin{document}
    {\scriptsize Text}, \first{Theorem}, \last{Definition}, \UP{small case text}, {\script{SOME text}},
    \getsc{Theorem}
\end{document}

I get the following output compilation. enter image description here

2 Answers 2

1

In other words, you want to fake \textsc, probably because your font doesn't sport small caps.

\documentclass{article}

\ExplSyntaxOn

\NewDocumentCommand{\fakesc}{m}
 {
  \text_uppercase:n { \tl_head:n { #1 } }
  {\scriptsize\text_uppercase:n { \tl_tail:n { #1 } }}
 }

\ExplSyntaxOff


\begin{document}

\fakesc{Theorem}

\fakesc{theorem}

\end{document}

enter image description here

Your approach doesn't work, because \StrLeft{#1}{1} is a set of instructions for producing (and printing) the first token.

If you want an implementation using xstring (which I don't recommend)

\documentclass{article}
\usepackage{xstring}

\newcommand{\getsc}[1]{%
  \StrLeft{#1}{1}[\headtoken]%
  \StrGobbleLeft{#1}{1}[\tailtokens]%
  \MakeUppercase{\headtoken\scriptsize\tailtokens}%
}

\begin{document}

\getsc{Theorem}

\getsc{theorem}

\end{document}
2
  • The xstring package worked but the first approach gave the following error: Missing number, treated as zero. ^^I\fakesc{Theorem} File ended while scanning use of __text_expand_loop:w.
    – Masum
    Commented Oct 4, 2021 at 13:43
  • @Masum I guess you need to update your TeX system.
    – egreg
    Commented Oct 4, 2021 at 13:56
1

There are many ways to do it, but I wouldn't use the xstring package in this case.

Using a delimited macro you can pick up the head and tail of a string of characters very easily.

\documentclass{article}
\begin{document}

\def\head#1{{\Huge #1}\ignorespaces}
\def\tail#1{{\scriptsize#1}}

\def\myfancymacro#1{%
        \def\temp##1##2;{%
             \head{##1}
             \tail{##2}
         }
   \expandafter\MakeUppercase{\temp#1;}
}

\myfancymacro{theorem}
\end{document}

There are many other ways. Both Xparse and the l3 modules have a plethora of commands that can acheve this.

2
  • both of you are saying xstring is not recommended. can you please elaborate on that?
    – Masum
    Commented Oct 4, 2021 at 13:45
  • 1
    Why add a library and another level of abstraction for such a simple problem? It also stops you from learning the basics of TeX/LaTeX programming. Once you have a bit of understanding, move on to l3 (what egreg used above in his first example). The l3 modules, offer a consistent interface and solve many common programming problems.
    – yannisl
    Commented Oct 4, 2021 at 18:33

You must log in to answer this question.

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