1

\Opt is a vector of 5 elements

enter image description here

but some of them can be left empty.

enter image description here

Based on this nice answer I learnt how to test if a parameter is empty or not.

But as you'll see in my MWE I struggle with spaces. I manually add an extra space in \Opt{EMTN }{SPX }{jun }{ATM }{C} but no doubt there is an automated answer to avoid double space when a parameter is empty and more harmoniously display the elements.

enter image description here

\documentclass{article}

\usepackage{xcolor}

\newcommand{\Opt}[5]{% {\color{blue!80}% \if\relax\detokenize{#1}\relax\else\texttt{#1}\fi } %I get a legit double space here if no % {\color{orange!80!black} \if\relax\detokenize{#2}\relax\else\texttt{#2}\fi \if\relax\detokenize{#3}\relax\else\texttt{#3}\fi \if\relax\detokenize{#4}\relax\else\texttt{#4}\fi \if\relax\detokenize{#5}\relax\else\texttt{#5}\fi } }

\begin{document}

\Opt{EMTN }{SPX }{}{}{C}

\Opt{EMTN }{SPX }{jun }{ATM }{C}

\Opt{EMTN}{SPX}{}{}{C}

\Opt{FUND}{AAPL}{jun}{OTM}{C}

\Opt{FUND }{AAPL }{jun }{OTM }{C}

\Opt{}{SPX }{}{}{Put}

\end{document}

JeT
  • 3,020
  • you are adding a space at the start { one after 80} one after } one after black} and another after } the % after \fi do nothing as white space after a command name is ignored, you need % on the other` lines to avoid adding space. – David Carlisle Mar 13 '21 at 01:22
  • @DavidCarlisle Right, I updated MWE and the image of the result. If I'm not wrong, I now get some double space. – JeT Mar 13 '21 at 01:36
  • 1
    you are still adding a space after the } at the end and an (ignored) space after {black} plusa space here } % that percent does nothing if you leave a space before it. If you comment out all the spaces you are adding and remove the spaces in the arguments you should get no space in the result, then add spaces after the \texttt{#2} as you know in that branch they are non empty – David Carlisle Mar 13 '21 at 01:48
  • 1
    Why not remove the space from the arguments and define thus: \newcommand{\Opt}[5]{{\color{blue!80}\if\relax\detokenize{#1}\relax\else\texttt{#1 }\fi }{\color{orange!80!black}\if\relax\detokenize{#2}\relax\else\texttt{#2 }\fi\if\relax\detokenize{#3}\relax\else\texttt{#3 }\fi\if\relax\detokenize{#4}\relax\else\texttt{#4 }\fi\if\relax\detokenize{#5}\relax\else\texttt{#5}\fi}}. The calls would be `\Opt{EMTN}{SPX}{}{}{C}

    \Opt{EMTN}{SPX}{jun}{ATM}{C}

    \Opt{EMTN}{SPX}{}{}{C}

    \Opt{FUND}{AAPL}{jun}{OTM}{C}

    \Opt{FUND}{AAPL}{jun}{OTM}{C}

    \Opt{}{SPX}{}{}{Put}`

    – Steven B. Segletes Mar 13 '21 at 01:56
  • 1
    Related: https://tex.stackexchange.com/q/69767/5764 – Werner Mar 13 '21 at 02:58

1 Answers1

2

You can add the space in \textt{#? } and do \unskip after the last parameter:

\def\Opt#1#2#3#4#5{%
   {\color{blue!80}% 
       \if\relax\detokenize{#1}\relax\else\texttt{#1 }\fi
   }%  I get a legit double space here if no %
   {\color{orange!80!black}% 
    \if\relax\detokenize{#2}\relax\else\texttt{#2 }\fi 
    \if\relax\detokenize{#3}\relax\else\texttt{#3 }\fi
    \if\relax\detokenize{#4}\relax\else\texttt{#4 }\fi
    \if\relax\detokenize{#5}\relax\else\texttt{#5 }\fi
   }%
   \unskip
}

test:

\Opt{EMTN}{SPX}{}{}{C}

\Opt{EMTN}{SPX}{jun}{ATM}{C}

\Opt{EMTN}{SPX}{}{}{C}

\Opt {EMTN} {SPX} {jun} {ATM} {C}

wipet
  • 74,238