1

Problem

I am trying to put a bunch of optional arguments in a new tcolorbox definition, and trying to use the ifmtarg so I can skip some optional arguments like in the following syntax:

Example 1: A tcolorbox with 6 arguments

Here is a tcolorbox with 6 arguments and how it is meant to be specified when leaving out the 5th argument (which is meant to be optional):

\begin{mybox}*[before title text]{mandatory title}[width][][extra options]

Here, the * acts as #1, before title text as #2, mandatory title as #3, width as #4, -NoValue- (empty) as #5, and extra options as #6.

In xargs terms, it would be like: { s O m O O O }.

I tried defining a new tcolorbox environment that does this (sort of following this answer), but I messed up and keep getting errors such as:

! LaTeX cmd Error: Bad argument specification ' s O{\textbf {Figure
(cmd)              \thetcbcounter :}~} m O O O ' for environment
(cmd)              'figurebox'.

For immediate help type H <return>.
 ...                                              
                                                  
l.46     }
          

The argument specification provided is not valid: one or more mandatory parts
are missing.

LaTeX will ignore this entire definition.

MWE

\documentclass[12pt, a4paper]{article}

% Layout
\usepackage[margin=2cm]{geometry}

% Colour
\usepackage[dvipsnames, cmyk]{xcolor}

% Graphing and math
\usepackage{amsmath}
\usepackage{pgfplots}
\pgfplotsset{%
    compat=1.18,
    trig format plots=rad,
    My Style 1/.style={%
            axis lines=middle,
            xlabel={$x$},
            ylabel={$y$},
            xtick=\empty,
            ytick=\empty,
            label style={font=\footnotesize},
            major tick style={semithick}
        },
}

% Arguments
\usepackage{ifmtarg}

% tcolorbox
\usepackage{tcolorbox}
\tcbuselibrary{%
    skins
}

%% Custom tcolorboxes
\NewTColorBox
    [auto counter, number within=part]
    {figurebox}
    { s O{\textbf{Figure \thetcbcounter:}~} m O O O }  % {1: left or not, 2: before title declaration, 3: title, 4: width, 5: colour, 6: extra options}
    {
        enhanced,
        title=#3,
        fonttitle=\scriptsize,
        colframe=\IfNoValueTF{#5}{Violet}{\makeatletter\@ifmtarg{#5}{Violet}{#5}\makeatother},
        colback=\IfNoValueTF{#5}{Violet!5!white}{\makeatletter\@ifmtarg{#5}{Violet}{#5!5!white}\makeatother},
        before title=#2,
        arc=3.5mm,
        sharp corners=downhill,
        width=\IfNoValueTF{#4}{6cm}{\makeatletter\@ifmtarg{#4}{6cm}{#4}\makeatother},
        boxsep=0pt,
        left=4pt,
        right=4pt,
        top=4pt,
        bottom=4pt,
        leftrule=\IfBooleanTF{#1}{3mm}{0.5mm},  % initial is 0.5mm
        rightrule=\IfBooleanTF{#1}{0.5mm}{3mm},  % initial is 0.5mm
        \IfNoValueTF{#6}{}{\makeatletter\@ifmtarg{#6}{}{#6}\makeatother}
    }

\title{\TeX{}.SE MWE}
\author{BrightBulb123}

\begin{document}

\maketitle

\section{Introduction}

\begin{figurebox}{Graph of \( \sin(x) \)}[\linewidth][][center upper, valign=center, halign=center, tikz upper]
    \begin{axis}[My Style 1, xmin=-4*pi, xmax=4*pi, ymin=-1.25, ymax=1.25, width=\linewidth]
        \addplot[black, thick, samples=400, domain=-4*pi:4*pi] {sin(x)};
    \end{axis}
\end{figurebox}

\end{document}
2
  • 2
    Erh, why not use the build in \IfBlankTF{var}{true}{false}?
    – daleif
    Commented Mar 15 at 13:17
  • 3
    If you need \makeatletter...\makeatother, place it around the whole macro definition, not within the definition. Commented Mar 15 at 13:22

1 Answer 1

2

The argument type O needs a default value: O{my default}, it's why you get an error. Use o if you don't need a default.

In your case, since you check if the argument is empty, it's preferable to use O{}. The many \IfNoValueTF are then useless.

Replacing \@ifmtarg with \IfBlankTF, as proposed by daleif, you can use:

\documentclass[12pt, a4paper]{article}

% Layout
\usepackage[margin=2cm]{geometry}

% Colour
\usepackage[dvipsnames, cmyk]{xcolor}

% Graphing and math
\usepackage{amsmath}
\usepackage{pgfplots}
\pgfplotsset{%
    compat=1.18,
    trig format plots=rad,
    My Style 1/.style={%
            axis lines=middle,
            xlabel={$x$},
            ylabel={$y$},
            xtick=\empty,
            ytick=\empty,
            label style={font=\footnotesize},
            major tick style={semithick}
        },
}

% tcolorbox
\usepackage{tcolorbox}
\tcbuselibrary{%
    skins
}

%% Custom tcolorboxes
\NewTColorBox
    [auto counter, number within=part]
    {figurebox}
    { s O{\textbf{Figure \thetcbcounter:}~} m O{} O{} O{} }  % {1: left or not, 2: before title declaration, 3: title, 4: width, 5: colour, 6: extra options}
    {
        enhanced,
        title=#3,
        fonttitle=\scriptsize,
        colframe=\IfBlankTF{#5}{Violet}{#5},
        colback=\IfBlankTF{#5}{Violet!5!white}{#5!5!white},
        before title=#2,
        arc=3.5mm,
        sharp corners=downhill,
        width=\IfBlankTF{#4}{6cm}{#4},
        boxsep=0pt,
        left=4pt,
        right=4pt,
        top=4pt,
        bottom=4pt,
        leftrule=\IfBooleanTF{#1}{3mm}{0.5mm},  % initial is 0.5mm
        rightrule=\IfBooleanTF{#1}{0.5mm}{3mm},  % initial is 0.5mm
        #6
    }

\title{\TeX{}.SE MWE}
\author{BrightBulb123}

\begin{document}

\maketitle

\section{Introduction}

\begin{figurebox}{Graph of \( \sin(x) \)}[\linewidth][][center upper, valign=center, halign=center, tikz upper]
    \begin{axis}[My Style 1, xmin=-4*pi, xmax=4*pi, ymin=-1.25, ymax=1.25, width=\linewidth]
        \addplot[black, thick, samples=400, domain=-4*pi:4*pi] {sin(x)};
    \end{axis}
\end{figurebox}

\end{document}

You must log in to answer this question.

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