问题

问题

问题

我正在尝试在新定义中放入一堆可选参数tcolorbox,并尝试使用,ifmtarg这样我就可以跳过一些可选参数,如以下语法:

示例 1:tcolorbox具有 6 个参数的A

这是一个具有 6 个参数的 tcolorbox,以及在省略第 5 个参数(该参数是可选的)时如何指定它:

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

这里,*充当#1before title text充当#2mandatory title充当#3width充当#4-NoValue-(空) 充当#5,并extra options充当#6

具体来说xargs,它将会是这样的:{ s O m O O O }

我尝试定义一个tcolorbox可以做到这一点的新环境(有点像这个答案),但我搞砸了并且不断收到错误,例如:

! 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.

平均能量损失

\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}

答案1

参数类型O需要默认值:O{my default},这就是出现错误的原因。o如果不需要默认值,请使用。

就你的情况而言,由于你要检查参数是否为空,因此最好使用O{}。 那么 many\IfNoValueTF就没用了。

\@ifmtarg按照 daleif 的建议,用替换\IfBlankTF,您可以使用:

\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}

相关内容