I would like to create a NewDocumentCommand
that would take optional arguments like font color (blue, red), font size (12pt, 18pt, maybe large, huge), and font style (bold, italic, maybe bold+italic). How to set up keyword options like bold and italic?
I have put together the following code, based on @egreg's answer to 338364/xparse-processor-with-multiple-arguments. I was able to get the color and font size to work, using color keywords from the xcolor package and font size in pt, but I'm not sure how best to go about setting optional keywords for the font style (say bf for bold, it for italic). I'm thinking of perhaps conditioning with \IfValueT
, but before I experiment I'd welcome pointers. Naturally it would be nice if this command could take math and color it and all that, but if that's too greedy, I'll settle for something that works with text.
\documentclass{article}
\usepackage{xparse,xcolor}
\usepackage{verbatim}
\ExplSyntaxOn
\keys_define:nn { mystyle }
{
color .tl_set:N = \l__mystyle_color_tl,
size .tl_set:N = \l__mystyle_size_tl,
skip .tl_set:N = \l__mystyle_skip_tl,
color .initial:n = red,
size .initial:n = 12pt,
skip .initial:n = 14.5pt,
}
\NewDocumentCommand{\stress}{O{}m}
{
\group_begin:
\keys_set:nn { mystyle } { #1 }
\clist_map_inline:nn { #2 }
{
\mystyle:VVVn
\l__mystyle_color_tl
\l__mystyle_size_tl
\l__mystyle_skip_tl
{##1}
}
\group_end:
}
\cs_new_protected:Nn \mystyle:nnnn
{
\color{#1}\fontsize{#2}{#3}\selectfont#4
}
\cs_generate_variant:Nn \mystyle:nnnn { VVV }
\ExplSyntaxOff
\begin{document}
\stress{emphasize this sentence with default values}
\stress[color=green,size=18pt]{emphasize this sentence with custom values}
\stress[color=blue,size=10pt,skip=50pt]{\textit{emphasize this sentence with custom values}}
Could I modify the command to:
\verb|\stress[style=it]{blabla}| to select italic,
\verb|\stress[style=bf]{blabla}| to select bold,
\verb|\stress[style=bfit]{blabla}| to select bold+italic
or even \verb|\stress[style=bf+it]{blabla}| to combine styles...
\end{document}
(the skip argument does not appear to work, but I suspect it's an easy fix for someone who knows what they're doing! maybe some sort of leavemode missing...)