如果没有给出标题,定理的 Tcolorbox 将以段落的第一段作为标题

如果没有给出标题,定理的 Tcolorbox 将以段落的第一段作为标题

我使用此代码作为我的代码的前言设置

\usepackage{fullpage}
\usepackage{amsmath,amsfonts}
\usepackage{enumitem}
\usepackage{hyperref}
\hypersetup{
    colorlinks=true,
    linkcolor=blue,
    filecolor=magenta,      
    urlcolor=cyan,
    pdftitle={Overleaf Example},
    pdfpagemode=FullScreen,
}
\usepackage[most,many]{tcolorbox}

\usepackage{xcolor}
\definecolor{myg}{RGB}{56, 140, 70}
\definecolor{myb}{RGB}{45, 111, 177}
\definecolor{myr}{RGB}{199, 68, 64}
\definecolor{mytheorembg}{HTML}{F2F2F9}
\definecolor{mytheoremfr}{HTML}{00007B}
\definecolor{myexamplebg}{HTML}{F2FBF8}
\definecolor{myexamplefr}{HTML}{88D6D1}
\definecolor{myexampleti}{HTML}{2A7F7F}
\definecolor{mydefinitbg}{HTML}{E5E5FF}
\definecolor{mydefinitfr}{HTML}{3F3FA3}
\definecolor{notesgreen}{RGB}{0,162,0}

\tcbuselibrary{theorems,skins,hooks}
\newtcbtheorem[number within=section]{Theorem}{Theorem}
{%
    enhanced
    ,colback = mytheorembg
    ,frame hidden
    ,boxrule = 0sp
    ,borderline west = {2pt}{0pt}{mytheoremfr}
    ,sharp corners
    ,detach title
    ,before upper = \tcbtitle\par\smallskip
    ,coltitle = mytheoremfr
    ,fonttitle = \bfseries\sffamily
    ,description font = \mdseries
    ,separator sign none
}
{th}
\newtcbtheorem[number within=section]{Example}{Example}
{%
    colback = myexamplebg
    ,colframe = myexamplefr
    ,coltitle = myexampleti
    ,boxrule = 1pt
    ,sharp corners
    ,detach title
    ,before upper=\tcbtitle\par\smallskip
    ,fonttitle = \bfseries
    ,description font = \mdseries
    ,separator sign none
    ,description delimiters parenthesis
}
{ex}



\usepackage{varwidth}
\newtcbtheorem[number within=section]{Definition}{Definition}{enhanced,
    before skip=2mm,after skip=2mm, colback=red!5,colframe=red!80!black,boxrule=0.5mm,
    attach boxed title to top left={xshift=1cm,yshift*=1mm-\tcboxedtitleheight}, varwidth boxed title*=-3cm,
    boxed title style={frame code={
            \path[fill=tcbcolback]
            ([yshift=-1mm,xshift=-1mm]frame.north west)
            arc[start angle=0,end angle=180,radius=1mm]
            ([yshift=-1mm,xshift=1mm]frame.north east)
            arc[start angle=180,end angle=0,radius=1mm];
            \path[left color=tcbcolback!60!black,right color=tcbcolback!60!black,
            middle color=tcbcolback!80!black]
            ([xshift=-2mm]frame.north west) -- ([xshift=2mm]frame.north east)
            [rounded corners=1mm]-- ([xshift=1mm,yshift=-1mm]frame.north east)
            -- (frame.south east) -- (frame.south west)
            -- ([xshift=-1mm,yshift=-1mm]frame.north west)
            [sharp corners]-- cycle;
        },interior engine=empty,
    },
    fonttitle=\bfseries,
    title={#2},#1}{def}

\newcommand{\thm}[2]{\begin{Theorem}{#1}a#2\end{Theorem}}
\newcommand{\ex}[2]{\begin{Example}{#1}a#2\end{Example}}
\newcommand{\dfn}[2]{\begin{Definition}[colbacktitle=red!75!black]{#1}a#2\end{Definition}}
\newcommand{\exs}[2]{\begin{Exercise}{#1}#2\end{Exercise}}
\newcommand{\eps}{\epsilon}
\newcommand{\veps}{\varepsilon}
\newcommand{\bbfR}{\boldsymbol{\mathbb{R}}}
\newcommand{\bs}[1]{\boldsymbol{#1}}


\makeatletter
\newtcbtheorem[number within=section]{Exercise}{Exercise}{enhanced, 
        breakable,
        colback=white,
        colframe=myb!80!black,
        attach boxed title to top left={yshift*=-\tcboxedtitleheight}, 
        title={#2},
        boxed title size=title,
        boxed title style={%
            sharp corners, 
            rounded corners=northwest, 
            colback=tcbcolframe, 
            boxrule=0pt,
        },
        underlay boxed title={%
            \path[fill=tcbcolframe] (title.south west)--(title.south east) 
            to[out=0, in=180] ([xshift=5mm]title.east)--
            (title.center-|frame.east)
            [rounded corners=\kvtcb@arc] |- 
            (frame.north) -| cycle; 
        },
        #1  
}{def}
\makeatother


\newcommand*\circled[1]{\tikz[baseline=(char.base)]{
        \node[shape=circle,draw,inner sep=1pt] (char) {#1};}}
%\newtcolorbox{myhbox}[2][]{%
%   myhbox={#1}{#2}
%}



\newtcolorbox{idioms}{
    breakable,
    enhanced,
    colback=white,
    colframe=idiomsgreen,
    arc=0pt,
    outer arc=0pt,
    title=Idioms,
    fonttitle=\bfseries\sffamily\large,
    colbacktitle=idiomsgreen,
    attach boxed title to top left={},
    boxed title style={
        enhanced,
        skin=enhancedfirst jigsaw,
        arc=3pt,
        bottom=0pt,
        interior style={fill=idiomsgreen}
    }
}
\usetikzlibrary{arrows,calc,shadows.blur}
\tcbuselibrary{skins}
\newtcolorbox{note}[1][]{%
    enhanced jigsaw,
    colback=gray!20!white,%
    colframe=gray!80!black,
    size=small,
    boxrule=1pt,
    title=\textbf{Note:-},
    halign title=flush center,
    coltitle=black,
    breakable,
    drop shadow=black!50!white,
    attach boxed title to top left={xshift=1cm,yshift=-\tcboxedtitleheight/2,yshifttext=-\tcboxedtitleheight/2},
    minipage boxed title=1.5cm,
    boxed title style={%
        colback=white,
        size=fbox,
        boxrule=1pt,
        boxsep=2pt,
        underlay={%
            \coordinate (dotA) at ($(interior.west) + (-0.5pt,0)$);
            \coordinate (dotB) at ($(interior.east) + (0.5pt,0)$);
            \begin{scope}
                \clip (interior.north west) rectangle ([xshift=3ex]interior.east);
                \filldraw [white, blur shadow={shadow opacity=60, shadow yshift=-.75ex}, rounded corners=2pt] (interior.north west) rectangle (interior.south east);
            \end{scope}
            \begin{scope}[gray!80!black]
                \fill (dotA) circle (2pt);
                \fill (dotB) circle (2pt);
            \end{scope}
        },
    },
    #1,
}

每当我使用定理 tcolorbox 的 wny 并将标题条目保留为空时,它都会将段落的第一个字母作为标题。如何防止这种情况

答案1

在里面包装文档,第 18.1 节,关于\newtcbtheorem,解释如下:

新环境 ⟨name⟩ 接受一个可选参数和两个强制参数。 ... omissis ... 第一个强制参数是定理的标题文本 ... omissis ... 第二个强制参数是 ⟨marker⟩。该定理自动标记为 ⟨prefix⟩⟨separator⟩⟨marker⟩,其中 ⟨separator⟩ 预定义为“:” ... omissis ...

因此,您的环境Theorem有两个必需参数。
如果其中一个不存在,则必须将其替换为{}
如果两个都不存在,则必须写入{}{}

\documentclass{book}

\usepackage{fullpage}
\usepackage{amsmath,amsfonts}
\usepackage{enumitem}
\usepackage{hyperref}
\hypersetup{
    colorlinks=true,
    linkcolor=blue,
    filecolor=magenta,      
    urlcolor=cyan,
    pdftitle={Overleaf Example},
    pdfpagemode=FullScreen,
}
\usepackage[most,many]{tcolorbox}

\usepackage{xcolor}
\definecolor{myg}{RGB}{56, 140, 70}
\definecolor{myb}{RGB}{45, 111, 177}
\definecolor{myr}{RGB}{199, 68, 64}
\definecolor{mytheorembg}{HTML}{F2F2F9}
\definecolor{mytheoremfr}{HTML}{00007B}
\definecolor{myexamplebg}{HTML}{F2FBF8}
\definecolor{myexamplefr}{HTML}{88D6D1}
\definecolor{myexampleti}{HTML}{2A7F7F}
\definecolor{mydefinitbg}{HTML}{E5E5FF}
\definecolor{mydefinitfr}{HTML}{3F3FA3}
\definecolor{notesgreen}{RGB}{0,162,0}

\tcbuselibrary{theorems,skins,hooks}
\newtcbtheorem[number within=section]{Theorem}{Theorem}
{%
    enhanced
    ,colback = mytheorembg
    ,frame hidden
    ,boxrule = 0sp
    ,borderline west = {2pt}{0pt}{mytheoremfr}
    ,sharp corners
    ,detach title
    ,before upper = \tcbtitle\par\smallskip
    ,coltitle = mytheoremfr
    ,fonttitle = \bfseries\sffamily
    ,description font = \mdseries
    ,separator sign none
}
{th}
\newtcbtheorem[number within=section]{Example}{Example}
{%
    colback = myexamplebg
    ,colframe = myexamplefr
    ,coltitle = myexampleti
    ,boxrule = 1pt
    ,sharp corners
    ,detach title
    ,before upper=\tcbtitle\par\smallskip
    ,fonttitle = \bfseries
    ,description font = \mdseries
    ,separator sign none
    ,description delimiters parenthesis
}
{ex}



\usepackage{varwidth}
\newtcbtheorem[number within=section]{Definition}{Definition}{enhanced,
    before skip=2mm,after skip=2mm, colback=red!5,colframe=red!80!black,boxrule=0.5mm,
    attach boxed title to top left={xshift=1cm,yshift*=1mm-\tcboxedtitleheight}, varwidth boxed title*=-3cm,
    boxed title style={frame code={
            \path[fill=tcbcolback]
            ([yshift=-1mm,xshift=-1mm]frame.north west)
            arc[start angle=0,end angle=180,radius=1mm]
            ([yshift=-1mm,xshift=1mm]frame.north east)
            arc[start angle=180,end angle=0,radius=1mm];
            \path[left color=tcbcolback!60!black,right color=tcbcolback!60!black,
            middle color=tcbcolback!80!black]
            ([xshift=-2mm]frame.north west) -- ([xshift=2mm]frame.north east)
            [rounded corners=1mm]-- ([xshift=1mm,yshift=-1mm]frame.north east)
            -- (frame.south east) -- (frame.south west)
            -- ([xshift=-1mm,yshift=-1mm]frame.north west)
            [sharp corners]-- cycle;
        },interior engine=empty,
    },
    fonttitle=\bfseries,
    title={#2},#1}{def}

\newcommand{\thm}[2]{\begin{Theorem}{#1}a#2\end{Theorem}}
\newcommand{\ex}[2]{\begin{Example}{#1}a#2\end{Example}}
\newcommand{\dfn}[2]{\begin{Definition}[colbacktitle=red!75!black]{#1}a#2\end{Definition}}
\newcommand{\exs}[2]{\begin{Exercise}{#1}#2\end{Exercise}}
\newcommand{\eps}{\epsilon}
\newcommand{\veps}{\varepsilon}
\newcommand{\bbfR}{\boldsymbol{\mathbb{R}}}
\newcommand{\bs}[1]{\boldsymbol{#1}}


\makeatletter
\newtcbtheorem[number within=section]{Exercise}{Exercise}{enhanced, 
        breakable,
        colback=white,
        colframe=myb!80!black,
        attach boxed title to top left={yshift*=-\tcboxedtitleheight}, 
        title={#2},
        boxed title size=title,
        boxed title style={%
            sharp corners, 
            rounded corners=northwest, 
            colback=tcbcolframe, 
            boxrule=0pt,
        },
        underlay boxed title={%
            \path[fill=tcbcolframe] (title.south west)--(title.south east) 
            to[out=0, in=180] ([xshift=5mm]title.east)--
            (title.center-|frame.east)
            [rounded corners=\kvtcb@arc] |- 
            (frame.north) -| cycle; 
        },
        #1  
}{def}
\makeatother


\newcommand*\circled[1]{\tikz[baseline=(char.base)]{
        \node[shape=circle,draw,inner sep=1pt] (char) {#1};}}
%\newtcolorbox{myhbox}[2][]{%
%   myhbox={#1}{#2}
%}



\newtcolorbox{idioms}{
    breakable,
    enhanced,
    colback=white,
    colframe=idiomsgreen,
    arc=0pt,
    outer arc=0pt,
    title=Idioms,
    fonttitle=\bfseries\sffamily\large,
    colbacktitle=idiomsgreen,
    attach boxed title to top left={},
    boxed title style={
        enhanced,
        skin=enhancedfirst jigsaw,
        arc=3pt,
        bottom=0pt,
        interior style={fill=idiomsgreen}
    }
}
\usetikzlibrary{arrows,calc,shadows.blur}
\tcbuselibrary{skins}
\newtcolorbox{note}[1][]{%
    enhanced jigsaw,
    colback=gray!20!white,%
    colframe=gray!80!black,
    size=small,
    boxrule=1pt,
    title=\textbf{Note:-},
    halign title=flush center,
    coltitle=black,
    breakable,
    drop shadow=black!50!white,
    attach boxed title to top left={xshift=1cm,yshift=-\tcboxedtitleheight/2,yshifttext=-\tcboxedtitleheight/2},
    minipage boxed title=1.5cm,
    boxed title style={%
        colback=white,
        size=fbox,
        boxrule=1pt,
        boxsep=2pt,
        underlay={%
            \coordinate (dotA) at ($(interior.west) + (-0.5pt,0)$);
            \coordinate (dotB) at ($(interior.east) + (0.5pt,0)$);
            \begin{scope}
                \clip (interior.north west) rectangle ([xshift=3ex]interior.east);
                \filldraw [white, blur shadow={shadow opacity=60, shadow yshift=-.75ex}, rounded corners=2pt] (interior.north west) rectangle (interior.south east);
            \end{scope}
            \begin{scope}[gray!80!black]
                \fill (dotA) circle (2pt);
                \fill (dotB) circle (2pt);
            \end{scope}
        },
    },
    #1,
}

\begin{document}
\chapter{My Chapter}
\section{My Section}
\begin{Theorem}{}{}
A theorem without title
\end{Theorem}
\begin{Theorem}{My title}{}
A theorem with title
\end{Theorem}
\end{document}

在此处输入图片描述

相关内容