尝试使用带有 \newcommand 的列表进入 tcolorbox

尝试使用带有 \newcommand 的列表进入 tcolorbox

我使用以下代码来列出练习,将每个练习放入一个 tcolorbox 中,但我找不到在序言中的 \newcommand 中自动枚举 tcolorbox 标题的方法。

\documentclass[a4paper,10pt]{article}
\usepackage{tcolorbox}

\newcommand{\exercise}{\begin{tcolorbox}[enhanced,title=
\begin{LARGE}
\color{purple}Exercise %<-- here I want automatically numbering%
\end{LARGE}  ,
colframe=black,colback=black!10!white,colbacktitle=black!8!white,drop shadow,
fonttitle=\bfseries,coltitle=black,attach boxed title to top center=
{yshift=-0.25mm-\tcboxedtitleheight/2,yshifttext=2mm-\tcboxedtitleheight/2},
boxed title style={boxrule=0.5mm,
frame code={ \path[tcb fill frame] ([xshift=-4mm]frame.west)
-- (frame.north west) -- (frame.north east) -- ([xshift=4mm]frame.east)
-- (frame.south east) -- (frame.south west) -- cycle; },
interior code={ \path[tcb fill interior] ([xshift=-2mm]interior.west)
-- (interior.north west) -- (interior.north east)
-- ([xshift=2mm]interior.east) -- (interior.south east) -- (interior.south west)
-- cycle;} }]}

\begin{document}
\exercise
random pronunciation
\end{tcolorbox}

\exercise %<-- second etc...
random pronunciation
\end{tcolorbox}

\end{document}

答案1

tcolorbox包裹允许自动对箱子进行编号:

\documentclass[a4paper,10pt]{article}
\usepackage[most]{tcolorbox}

\newtcolorbox[auto counter]{exercise}{
  enhanced,
  title=Exercise~\thetcbcounter,
  fonttitle=\LARGE\bfseries,
  coltitle=purple,
  colframe=black,
  colback=black!10!white,
  colbacktitle=black!8!white,
  drop shadow,
  attach boxed title to top center={yshift=-0.25mm-\tcboxedtitleheight/2,yshifttext=2mm-\tcboxedtitleheight/2},
  boxed title style={
    boxrule=0.5mm,
    frame code={ \path[tcb fill frame] ([xshift=-4mm]frame.west) -- (frame.north west) -- (frame.north east) -- ([xshift=4mm]frame.east) -- (frame.south east) -- (frame.south west) -- cycle; },
    interior code={ \path[tcb fill interior] ([xshift=-2mm]interior.west) -- (interior.north west) -- (interior.north east) -- ([xshift=2mm]interior.east) -- (interior.south east) -- (interior.south west) -- cycle;} 
  }
}

\begin{document}
\begin{exercise}
random pronunciation
\end{exercise}

\begin{exercise}
random pronunciation
\end{exercise}

\end{document}

在此处输入图片描述

答案2

定义一个计数器并在每次时对其进行步进exercise

我从 改为\newcommand\newenvironment获得更好的符合语法。

\documentclass[a4paper,10pt]{article}
\usepackage[many]{tcolorbox}

\newcounter{exercise}

\newenvironment{exercise}{%
  \stepcounter{exercise}
  \begin{tcolorbox}[
    enhanced,
    title=\textcolor{purple}{Exercise \theexercise},
    colframe=black,
    colback=black!10!white,
    colbacktitle=black!8!white,
    drop shadow,
    fonttitle=\bfseries\LARGE,
    coltitle=black,
    attach boxed title to top center={
      yshift=-0.25mm-\tcboxedtitleheight/2,
      yshifttext=2mm-\tcboxedtitleheight/2},
      boxed title style={
        boxrule=0.5mm,
        frame code={
          \path[tcb fill frame] ([xshift=-4mm]frame.west)
            -- (frame.north west) -- (frame.north east) -- ([xshift=4mm]frame.east)
            -- (frame.south east) -- (frame.south west) -- cycle;
        },
        interior code={
          \path[tcb fill interior] ([xshift=-2mm]interior.west)
            -- (interior.north west) -- (interior.north east)
            -- ([xshift=2mm]interior.east) -- (interior.south east) -- (interior.south west)
            -- cycle;
        },
      },
    ]%
}{\end{tcolorbox}}

\begin{document}

\begin{exercise}
random pronunciation
\end{exercise}

\begin{exercise}
random pronunciation
\end{exercise}

\end{document}

在此处输入图片描述

相关内容