4

I've written a macro to use some command like a ltxdoc (\oarg,\meta,\marg) without using the textcomp and ltxdocto colorate and decide the font style with which the angle, brackets and text in command. I would like to write the command of the form:

\mycomand[key=value]{...}

But it has been difficult to me because of the selection of sources font for typewrite and roman. This is the file (MWE)

% 2015-29-04
\documentclass{article}
\usepackage[T1]{fontenc}%
\usepackage{libertine}%
\usepackage[dvipsnames]{xcolor}
\usepackage{listings}
% Font used for \textangle in \ttfamily
\DeclareFontEncoding{TS1}{}{}
\DeclareFontSubstitution{TS1}{cmr}{m}{n}
\DeclareTextSymbol{\ttlangle}{TS1}{60}
\DeclareTextSymbol{\ttrangle}{TS1}{62}
\DeclareTextSymbolDefault{\ttlangle}{TS1}
\DeclareTextSymbolDefault{\ttrangle}{TS1}
% adapted from doc.dtx    
\providecommand\meta[1]{{\fontfamily{cmr}\fontencoding{TS1}\selectfont\ttlangle\/}\ignorespaces{\normalfont\itshape #1\/}{\fontfamily{cmr}\fontencoding{TS1}\selectfont\/\ttrangle}} % close
% adapted from ltxdoc.dtx
\providecommand\marg[1]{{\fontfamily{cmr}\fontencoding{T1}\selectfont\{\ignorespaces}\normalfont\meta{#1}\ignorespaces{\fontfamily{cmr}\fontencoding{T1}\selectfont\}\ignorespaces}} % close
\providecommand\oarg[1]{{\fontfamily{cmr}\fontencoding{T1}\selectfont [}\normalfont\meta{#1}{\fontfamily{cmr}\fontencoding{T1}\selectfont ]\ignorespaces}} % close
\begin{document}
\noindent
ROMAN

\textlangle\emph{meta}\textrangle \par
[\textlangle\emph{oarg}\textrangle] \par
\{\textlangle\emph{marg}\textrangle\} \par

\noindent
TYPEWRITE 

\meta{meta} \par
\oarg{oarg} \par
\marg{marg} \par

\let\us\textunderscore
\newcommand\placeholder[1]%
{%
    \bgroup
        \normalfont\upshape\color{RubineRed}%
        \textlangle{\itshape #1\/}\textrangle%
    \egroup
}

\lstset
{%
    basicstyle  = \ttfamily,
        escapeinside={<}{>},%
}

\noindent
TEST USING ROMAN 
\begin{lstlisting}
for i= <\placeholder{first\us{}value}>:<\placeholder{last\us{}value}>
    % do stuff
end
\end{lstlisting}

\noindent
TEST USING TYPEWRITER
\begin{lstlisting}
for i= <\meta{first\us{}value}>:<\meta{last\us{}value}>
    % do stuff
end
\end{lstlisting}

TRY

\begin{lstlisting}
\mymeta[type=<\meta{tt,rm}>,cf=<\meta{color font}>,ac=<\meta{angle color}>]{ ... }
\myoarg[type=<\meta{tt,rm}>,cf=<\meta{color font}>,ac=<\meta{angle color}>,sbc=<\meta{square brackets color}>]{ ... }
\mymarg[type=<\meta{tt,rm}>,cf=<\meta{color font}>,ac=<\meta{angle color}>,cbc=<\meta{curly braces color}>]{ ... }
\end{lstlisting}
\end{document}

My idea would be to let the default typewrite font and everything in black color.

Edit: Attempt to define the keys:

type= text font (tt or rm) (default tt)
cf  = color font (default black)
ac  = angle color (default black)
sbc = square brackets color (default black)
cbc = curly braces color (default black)

And command(s)

% use \meta
\mymeta[type=tt,cf=red,ac=green}]{ ... }
% use \oarg
\myoarg[type=tt,cf=red,ac=green,sbc=blue]{ ... }
% use \marg
\mymarg[type=tt,cf=red,ac=green,cbc=blue]{ ... }

Thankful in advance.

  • What do you want to specify with the keys? The description you give in the last lines doesn't tell much. – egreg Apr 29 '15 at 13:48
  • @egreg: thanks for the reply , I edited the post to clarify the idea. – Pablo González L Apr 29 '15 at 14:38
3

Here's a possible implementation:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{textcomp}
%\usepackage{libertine}
\usepackage[dvipsnames]{xcolor}
\usepackage{xparse}

\ExplSyntaxOn
% user level commands
\NewDocumentCommand{\meta}{O{}m}
 {
  \pablo_meta_generic:Nnn \pablo_meta:n { #1 } { #2 }
 }
\NewDocumentCommand{\marg}{O{}m}
 {
  \pablo_meta_generic:Nnn \pablo_marg:n { #1 } { #2 }
 }
\NewDocumentCommand{\oarg}{O{}m}
 {
  \pablo_meta_generic:Nnn \pablo_oarg:n { #1 } { #2 }
 }

% variables and keys
\tl_new:N \l_pablo_meta_font_tl

\keys_define:nn { pablo/meta }
 {
  type .choice:,
  type / tt .code:n = \tl_set:Nn \l_pablo_meta_font_tl { \ttfamily },
  type / rm .code:n = \tl_set:Nn \l_pablo_meta_font_tl { \rmfamily },
  type .initial:n = tt,
  cf .tl_set:N = \l_pablo_meta_color_tl,
  cf .initial:n = black,
  ac .tl_set:N = \l_pablo_meta_anglecolor_tl,
  ac .initial:n = black,
  sbc .tl_set:N = \l_pablo_meta_brackcolor_tl,
  sbc .initial:n = black,
  cbc .tl_set:N = \l_pablo_meta_bracecolor_tl,
  cbc .initial:n = black,
 }


% internal commands
\cs_new_protected:Npn \pablo_meta_generic:Nnn #1 #2 #3
 {
  \group_begin:
  \keys_set:nn { pablo/meta } { #2 }
  \color{ \l_pablo_meta_color_tl }
  \l_pablo_meta_font_tl
  #1 { #3 } % #1 is \pablo_meta:n, \pablo_marg:n or \pablo_oarg:n
  \group_end:
 }
\cs_new_protected:Npn \pablo_meta:n #1
 {
  \pablo_meta_angle:n { \textlangle }
  \pablo_meta_meta:n { #1 }
  \pablo_meta_angle:n { \textrangle }
 }
\cs_new_protected:Npn \pablo_marg:n #1
 {
  \pablo_meta_brace:n { \textbraceleft }
  \pablo_meta:n { #1 }
  \pablo_meta_brace:n { \textbraceright }
 }
\cs_new_protected:Npn \pablo_oarg:n #1
 {
  \pablo_meta_brack:n { [ }
  \pablo_meta:n { #1 }
  \pablo_meta_brack:n { ] }
 }
\cs_new_protected:Npn \pablo_meta_meta:n #1
 {
  \textnormal{\textit{#1}}
 }
\cs_new_protected:Npn \pablo_meta_angle:n #1
 {
  \group_begin:
  \fontfamily{cmr}\selectfont
  \textcolor{\l_pablo_meta_anglecolor_tl}{#1}
  \group_end:
 }
\cs_new_protected:Npn \pablo_meta_brace:n #1
 {
  \group_begin:
  \color{\l_pablo_meta_bracecolor_tl}
  #1
  \group_end:
 }
\cs_new_protected:Npn \pablo_meta_brack:n #1
 {
  \textcolor{\l_pablo_meta_brackcolor_tl}{#1}
 }
\ExplSyntaxOff


\begin{document}

\noindent
\meta{something}\\
\marg{mandatory argument}\\
\oarg{optional argument}

\noindent
\meta[cf=red,ac=green]{something}\\
\marg[type=rm,cbc=red]{mandatory argument}\\
\oarg[type=rm,sbc=blue]{optional argument}

\end{document}
\noindent
\meta{something}\\
\marg{mandatory argument}\\
\oarg{optional argument}

\noindent
\meta{something}\\
\marg{mandatory argument}\\
\oarg{optional argument}

\noindent
\meta{something}\\
\marg{mandatory argument}\\
\oarg{optional argument}

\end{document}

I commented out libertine because the difference between Roman and monospaced fonts is better seen with Computer Modern.

enter image description here

Note that \meta, \marg and \oarg are defined in terms of a generic command that does the setting of the keys and calls the appropriate internal command. All items are defined with a particular macro, so to achieve uniformity.

| improve this answer | |
  • Thanks for the reply. I trying not to use the textcomp package, i need if the option tt load { } , [] load from typewrite font and the symbol \textlangle and \textrangle from \DeclareTextSymbolDefault{\ttlangle}{TS1} and \DeclareTextSymbolDefault{\ttrangle}{TS1} . Is it possible to do this ?. Loading tetcomp take \textlangleand \textrangledirect from font if available (libertine for example) and does not look good when you want to write in a context verbatim. Saludos – Pablo González L Apr 29 '15 at 17:02
  • @PabloGonzálezL With my code the angle brackets are taken from Computer Modern. – egreg Apr 29 '15 at 17:46
  • Thank you so much , is exactly what I wanted, works perfectly whit angle braquets, ...Will it work on luatex and xetex too? – Pablo González L Apr 29 '15 at 18:29
  • @PabloGonzálezL You'd have just to modify the definition of the angle brackets. – egreg Apr 29 '15 at 20:57
  • ok , that would be another question. thanks for everything. – Pablo González L Apr 29 '15 at 22:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.