0

I'm designing 2 macros, and nesting them fails and I can't see where I've made a mistake.

The 1st macro takes a floating point number (eg 3.2) and check it agains 3 thresholds. The second macro displays a tag, depending on the threshold.

Both macro work separately, but nesting them brings me an error I can't solve :

! Incomplete \iffalse; all text was ignored after line 61.
<inserted text> 
\fi 

Here is the code :

\documentclass{article}
\usepackage{xcolor}
\usepackage[skins]{tcolorbox}
\tcbuselibrary{xparse}

\newcommand*{\ScoreHigh}{7.0}
\newcommand*{\ScoreLow}{3.0}

% Will output None, Low or High depending on the input
\newcommand{\category}[1]{%
    \pgfmathparse{#1 < \ScoreLow}
    \ifnum\pgfmathresult=1
        None%
    \else
        \pgfmathparse{#1 < \ScoreHigh}
        \ifnum\pgfmathresult=1
            Low%
        \else
            High%
        \fi%
    \fi%
}

\colorlet{color@None}{black!20}
\definecolor{color@Low}{rgb}{0.0, 0.65, 0.31}
\colorlet{color@High}{orange}

\DeclareTotalTCBox{\Tag}{m}{
    enhanced,nobeforeafter,
    tcbox raise base,
    boxrule=0.4pt,
    top=0mm,bottom=0mm,right=1mm,left=1mm,
    boxsep=2pt,
    colframe=color@#1,
    colback=tcbcolframe,
    coltext=black,
}{#1}%

\MakeRobust\Tag

\ifdefined\pdfstringdefDisableCommands
\pdfstringdefDisableCommands{\def\Tag#1{'#1'}}
\fi

\setlength{\parindent}{0pt}

\begin{document}

$5.3/10 \rightarrow$  category \textbf{\category{5.3}}.

\bigskip

This is a \Tag{Low} tag.

\Tag{\category{5.3}} % <--- Chaining these command fail
    
   
\end{document}

Indeed, the error is related to the line 61, where the macros are nested.

Any ideas why I might fail ?

1
  • 1
    your category command is not expandable, so you can't use it for the color in colframe=color@#1,. Sep 15, 2021 at 14:22

1 Answer 1

2

pgf calculations are not expandable. But you can use expl3 and l3fp instead:

\documentclass{article}
\usepackage{xcolor}
\usepackage[skins]{tcolorbox}
\tcbuselibrary{xparse}

\newcommand*{\ScoreHigh}{7.0}
\newcommand*{\ScoreLow}{3.0}

% Will output None, Low or High depending on the input
\ExplSyntaxOn
\newcommand{\category}[1]{
 \fp_compare:nNnTF {#1}<{\ScoreLow}{None}
  {
    \fp_compare:nNnTF{#1}<{\ScoreHigh}
     {Low}{High}
  }}
\ExplSyntaxOff
\colorlet{color@None}{black!20}
\definecolor{color@Low}{rgb}{0.0, 0.65, 0.31}
\colorlet{color@High}{orange}

\DeclareTotalTCBox{\Tag}{m}{
    enhanced,nobeforeafter,
    tcbox raise base,
    boxrule=0.4pt,
    top=0mm,bottom=0mm,right=1mm,left=1mm,
    boxsep=2pt,
    colframe=color@#1,
    colback=tcbcolframe,
    coltext=black,
}{#1}%

\MakeRobust\Tag

\ifdefined\pdfstringdefDisableCommands
\pdfstringdefDisableCommands{\def\Tag#1{'#1'}}
\fi

\setlength{\parindent}{0pt}

\begin{document}
\Tag{\category{5.3}} 
\Tag{\category{-1}}
\Tag{\category{8}}

\end{document}

enter image description here

You must log in to answer this question.

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