Quantcast
Viewing latest article 1
Browse Latest Browse All 3

Answer by egreg for Formatting Strings in Dynamic Variables with xstring

Here's a modification of the former solution that uses a different workaround:

\documentclass{article}\usepackage{amsmath}\usepackage{color}\usepackage{xstring}\makeatletter\newcommand{\ChgFmt}[1]{%  \ensuremath{%    \begingroup    \expandarg    \protected@edef\temp{#1}%    \IfSubStr{\temp}{-}      {\StrSubstitute{\temp}{-}{}[\temp]\@firstoftwo}      {\@secondoftwo}%    {\color{red}{\downarrow}\temp\endgroup}    {\color{green}{\uparrow}\temp\endgroup}%  }%}\newcommand{\FirstDollarAmount}{\$1\text{ million}}\newcommand{\SecondDollarAmount}{-\$1\text{ million}}\newcommand{\inputnum}[2]{\expandafter\csname #1#2\endcsname}\begin{document}This number is printed in green: \ChgFmt{\$1 \text{ million}}This number is printed in red: \ChgFmt{-\$1 \text{ million}}This number should be printed in green: \ChgFmt{\inputnum{First}{DollarAmount}}This number should be printed in red: \ChgFmt{\inputnum{Second}{DollarAmount}}\end{document} 

Image may be NSFW.
Clik here to view.
enter image description here


Viewing latest article 1
Browse Latest Browse All 3

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>