设置 tcolorbox 中计数器的值

设置 tcolorbox 中计数器的值

我想将颜色框的计数器设置dtn为 3,以便它可以像类型 3、类型 4 等一样进行编译。但是,当我使用该命令时,setcounter{dtn}{2}会出现一些错误。有人能帮我解决这个问题吗?非常感谢你们

这是我的代码

\documentclass[a4paper,12pt, twoside]{article}
\usepackage[dvipsnames]{xcolor}
\usepackage[utf8]{vietnam}
\usepackage{wrapfig} 
\usepackage[top=2cm, bottom=2cm, left=1.5cm, right=1.5cm]{geometry}
\usepackage{color,graphicx,multicol,multirow,hyperref,geometry,graphicx,amsmath,tikz,amssymb,pgf,tkz-tab,amsfonts,amsthm,array,fancyhdr}
\usepackage{lipsum}
\usepackage{amsxtra, latexsym, amscd, enumerate, ifthen, amstext}
\usepackage{array, tabularx, longtable}
\usepackage{mathrsfs}
\usepackage[all]{xy}
\usepackage{subcaption}
\usepackage{csquotes}
\usetikzlibrary{arrows,calc}
\usetikzlibrary{decorations.pathmorphing,patterns}
\DeclareRobustCommand{\divby}{\mathrel{\vbox{\baselineskip.65ex\lineskiplimit0pt\hbox{.}\hbox{.}\hbox{.}}}}
\DeclareGraphicsExtensions{.pdf,.jpg,.png,.bmp}
\let\mod=\undefined
\let\top=\undefined
 \DeclareMathOperator{\im}{Im}
 \DeclareMathOperator{\Ker}{Ker}
\DeclareMathOperator{\rk}{rank} 
%Tiếng Anh
\newtheorem{theo}{Theorem}[section]
\newtheorem{defi}{Definition}[section]
\newtheorem{cor}{Corollary}[section]
\newtheorem{lem}{Lemma}[section]
\newtheorem{prop}{Proposition}[section]
\newtheorem{exam}{Example}[section]
\newtheorem{rema}{Remark}[section]
\newtheorem{construction}{Construction}[section]
\newtheorem{prob}{Problem}[section]
\newtheorem{ques}{Question}
\newtheorem{exe}{Exercise}

%Tiếng Việt
\newtheorem{theorem}{Định lý}[section]
\newtheorem{theorem1}{Định lý}
\newtheorem{lemma}{Bổ đề}[section]
\newtheorem{exercise}{Bài tập}[section]
\newtheorem{corollary}{Hệ quả}[section]
\newtheorem{proposition}{Mệnh đề}[section]
\newtheorem{remark}{Chú ý}[section]
\newtheorem{comment}{Nhận xét}[section]
\newtheorem{property}{Tính chất}[section]
\newtheorem{definition}{Định nghĩa}[section]
\newtheorem{example}{Ví dụ}[section]
\newtheorem{ex}{Ví dụ}
\newtheorem{bt}{Bài tập}
\newtheoremstyle{mystyle}% Name of the style
  {\topsep}% Space above
  {\topsep}% Space below
  {}% Body font
  {0pt}% Indent amount
  {\bfseries}% Theorem head font
  {}% Punctuation after theorem head (in this case empty because defined in the head spec)
  {.5em}% Space after theorem head
  {\ifstrequal{#3}{done}{\color{Blue}}{}\thmname{#1}\thmnumber{ #2.}}% Theorem head spec
\theoremstyle{mystyle}
\newtheorem{bto}{Bài}
\newtheorem{baitoan}{Bài toán}[section]
\newtheorem{md}{Mệnh đề}
\newtheorem{dl}{Định lý}
\newtheorem*{dn}{Định nghĩa.}
\newtheorem*{vd}{Ví dụ.}
\newtheorem{hq}{Hệ quả}
\newtheorem*{nx}{Nhận xét.}
\newtheorem*{bd}{Bổ đề.}
\newtheorem{cy}{Chú ý}
\newtheorem{ch}{Câu}
\newtheorem{dang}{Dạng}
\newtheorem{GT}[dl]{Assumption}

\newcommand{\Np}{\mathbb{N^*}}
\newcommand{\C}{\mathbb{C}}
\newcommand{\bsq}{\textnormal{\scalebox{2.5}{$\square$}}}
\newcommand{\Z}{\mathbb{Z}}
\newcommand{\N}{\mathbb{N}}
\newcommand{\Q}{\mathbb{Q}} 
\newcommand{\R}{\mathbb{R}} 
\newcommand{\pr}{\textnormal{P}}
\newcommand{\E}{\textnormal{E}}
\newcommand{\ans}{\textcolor{black!20}{\rule{\textwidth}{.5pt}}}
\renewcommand{\baselinestretch}{1.3}
\newcommand{\parallelsum}{\mathbin{\!/\mkern-5mu/\!}}
\usepackage{centernot} %viết dấu gạch chéo ở mũi tên
\usepackage{indentfirst}
\usepackage{setspace}
\usepackage[shortlabels]{enumitem}
%\setlist{leftmargin=5mm}
%Tạo frame
\usepackage[most]{tcolorbox}
\renewcommand*{\thesection}{\Roman{section}}
\renewcommand*{\thesubsection}{\arabic{subsection}}
\renewcommand\familydefault{\sfdefault}
%san - mathversion
%\DeclareMathVersion{sans}
%\SetSymbolFont{operators}{sans}{OT1}{cmbr}{m}{n}
%\SetSymbolFont{letters}{sans}{OML}{cmbrm}{m}{it}
%\SetSymbolFont{symbols}{sans}{OMS}{cmbrs}{m}{n}
%\SetMathAlphabet{\mathit}{sans}{OT1}{cmbr}{m}{sl}
%\SetMathAlphabet{\mathbf}{sans}{OT1}{cmbr}{bx}{n}
%\SetMathAlphabet{\mathtt}{sans}{OT1}{cmtl}{m}{n}
%\SetSymbolFont{largesymbols}{sans}{OMX}{iwona}{m}{n}
%\sffamily\mathversion{sans}
\newcolumntype{P}[1]{>{\centering\arraybackslash}p{#1}}

\newtcolorbox[auto counter]{dtn}[2][]{%
colback=white,colframe=Blue!75!black,fonttitle=\bfseries,
title=Type~\thetcbcounter. #2,#1}
\usepackage{fancyhdr,lipsum}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\usepackage{mathrsfs}
\usetikzlibrary{arrows}

%choice in multiple choice answer
\usepackage{tasks,calc}
\settasks{label = \protect\circled{\textbf{\sffamily\Alph*}},
  label-width = 2em,
  label-offset = 0pt,
  item-indent = 2em+4pt}
\usepackage{tikz}
\newcommand*\circled[1]{\tikz[baseline=(char.base)]{
            \node[shape=circle,draw,inner sep=1pt] (char) {#1};}}
%dotted line
\begin{document}
\begin{dtn}
something
\end{dtn}
\end{document}

答案1

该选项创建的计数器tcolorbox auto counter具有以下形式:

tcb@cnt@<boxname>

因此,我们可以轻松创建一个宏来设置任何特定框的计数器。它的语法如下

\setboxcounter{boxname}{value}

其定义是:

\newcommand\setboxcounter[2]{\setcounter{tcb@cnt@#1}{#2}}

这是您的示例(经过大量修剪以达到最小):

\documentclass[a4paper,12pt, twoside]{article}
\usepackage[dvipsnames]{xcolor}
\usepackage{tcolorbox}
\makeatletter
\newcommand\setboxcounter[2]{\setcounter{tcb@cnt@#1}{#2}}
\makeatother

\newtcolorbox[auto counter]{dtn}[2][]{%
colback=white,colframe=Blue!75!black,fonttitle=\bfseries,
title=Type~\thetcbcounter. #2,#1}

\begin{document}
\setboxcounter{dtn}{4}
\begin{dtn}{Title}
something
\end{dtn}
\end{document}

代码输出

相关内容