1

I want to display random strings made up of 's' and 'f' such that every s is white on a blue background and every f is white on a red background. I can make it work fine in normal text, but when I want to display it in \scriptsize it seems to not get small enough.

The code I'm using is:

\newcommand{\ss}[0]{\colorbox{blue}{\textcolor{white}{{\texttt{\scriptsize{s\strut}}}}}}
\newcommand{\ff}[0]{\colorbox{red}{\textcolor{white}{{\texttt{\scriptsize{f\strut}}}}}}

and then when I want to show a string I write something like:

\ss\ff\ss\ff\ff\ff\ss\ss
1
  • 1
    Could it do with the fact that \ss is an already defined LaTeX macro, so your code is generating errors? Correcting that, this seems to work fine: \documentclass{article} \usepackage{xcolor} \newcommand{\sss}[0]{\colorbox{blue}{\textcolor{white}{{\texttt{\scriptsize{s\strut}}}}}} \newcommand{\ff}[0]{\colorbox{red}{\textcolor{white}{{\texttt{\scriptsize{f\strut}}}}}} \begin{document} sf \sss\ff\sss\ff\ff\ff\sss\sss \end{document} Commented Nov 5, 2019 at 10:58

2 Answers 2

2

I think it's better to use a simpler macro, as far as usage is concerned.

\documentclass{article}
\usepackage{xparse,xcolor}

\ExplSyntaxOn

\NewDocumentCommand{\fs}{m}
 {
  \tl_map_function:nN { #1 } \firth_fs:n
 }

\cs_new_protected:Nn \firth_fs:n
 {
  \str_case:nn { #1 }
   {
    {f}{\__firth_fs_box:nn { red } { f }}
    {s}{\__firth_fs_box:nn { blue } { s }}
    % add more cases if needed
   }
 }
\cs_new_protected:Nn \__firth_fs_box:nn
 {
  \colorbox{#1}
   {
    \color{white}
    \use:c { check@mathfonts }
    \fontsize { \use:c{ sf@size } } { 0 } \selectfont
    #2 \vphantom{f} % possibly 'fy' if letters with descenders are used
   }
 }

\ExplSyntaxOff

\begin{document}

X \fs{sfsfffs} X

\medskip

\Large X \fs{sfsfffs} X

\medskip

\footnotesize X \fs{sfsfffs} X

\end{document}

enter image description here

You can even enter

\fs{s f s fff s}

as spaces are ignored.

4
  • In my humble opinion, all your answers are beautiful. Just an aesthetic consideration. Would the position of the letters inside the coloured boxes not be an optimal solution if they were aligned with the two X's? My best regards.
    – Sebastiano
    Commented Nov 5, 2019 at 17:03
  • 1
    @Sebastiano That depends on what's the intended usage. The X's are just to show the size relationships.
    – egreg
    Commented Nov 5, 2019 at 17:14
  • Thanks this is amazing! But one small thing- it doesn't work inside tables (just shows black text on white background). How can I make this work inside my tables (I use booktabs for tables, if thats important)?
    – John Firth
    Commented Nov 5, 2019 at 23:49
  • @JohnFirth I tried a simple tabular and see no issue.
    – egreg
    Commented Nov 6, 2019 at 8:26
2

Welcome here!

As said in the comments, \ss is already defined, so you need either to redefine it or define another command for printing the s.

However, \strut is a \vrule of 8.5pt height, 3.5pt depth and a width of 0pt.

That doesn't fit automagically to scriptsize. But you can adapt \strut:

\documentclass{article}

\usepackage{xcolor}

\renewcommand{\strut}{\vrule height5pt depth0pt width0pt}
\renewcommand{\ss}[0]{\colorbox{blue}{\textcolor{white}{{\texttt{\scriptsize{s\strut}}}}}}
\newcommand{\ff}[0]{\colorbox{red}{\scriptsize\textcolor{white}{{\texttt{{f\strut}}}}}}



\begin{document}
\ss\ff\ss\ff\ff\ff\ss\ss
\end{document}

See:

pic of code compiled

However, changing \strut or redefining \ss may have unwanted side effects. Maybe instead of redefining strut, give it another name \turts, e.g. Same for \ss.

1
  • Thanks, I didn't know strut worked like that.
    – John Firth
    Commented Nov 5, 2019 at 23:50

You must log in to answer this question.

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