tcolorbox 在单独的环境中从另一个 tcolorbox 中获取样式值

tcolorbox 在单独的环境中从另一个 tcolorbox 中获取样式值

我有两个环境,tip 和 codeheading,每个环境都有自己的 tcolorboxes,如下所示:

    \newenvironment{tip}
        {\tcolorbox[colback=tip-green-background, frame hidden, boxrule=0pt, boxsep=0pt, breakable, enlarge bottom by=0.3cm, enhanced jigsaw, borderline west={3pt}{0pt}{tip-green-border}, title={Tip \\[1mm]}, colbacktitle={tip-green-border}, coltitle={tip-green-text}, coltext={tip-green-text}, fonttitle={\small\bfseries}, attach title to upper={}]}
        {\endtcolorbox}
    \newenvironment{codeheading}[1]
        {\tcolorbox[colback=code-filename-background, every box, sharp corners=south, enlarge bottom by=-2.5mm, boxrule=0pt, boxsep=0pt, breakable, enhanced jigsaw, title={#1}, colbacktitle={black}, coltitle={white}, fonttitle={\small\bfseries}, attach title to upper={}]}
        {\endtcolorbox}

我注意到tip环境 tcolorbox 正在拾取在环境中定义的风格值codeheading

例如,codeheadingsharp corners=south,那么tip也有。如果我将此值从南改为北,则尖端的角也会改变。

为什么两个 tcolorbox 会拾取彼此的样式值?就好像在页面上使用颜色框后,每个颜色框都会开始拾取彼此的样式。它们会感到困惑吗?

我也在使用 Pandoc。

---
geometry: margin=3cm
mainfont: SF Pro Display
mainfontoptions: BoldFont=SF Pro Display Bold
monofont: Fira Code
fontsize: 11pt
header-includes:
    \usepackage{setspace}
    \spacing{1.25}
    \usepackage{fancyhdr}
    \renewcommand{\headrulewidth}{0pt}
    \pagestyle{fancy}
    \fancyhead[CO,CE]{This is fancy}
    \fancyhead[LE,RO]{\thepage}
    \fancyfoot[C]{Blah blah blah}
    \usepackage{blindtext}
    \usepackage[most]{tcolorbox} 
    \usepackage{changepage}
    \definecolor{tip-green-text}{HTML}{165c26}
    \definecolor{tip-green-background}{HTML}{dcffe4}
    \definecolor{tip-green-border}{HTML}{96f1b5}
    \definecolor{code-filename-background}{HTML}{161621}
    \newenvironment{tip}
        {\tcolorbox[colback=tip-green-background, frame hidden, boxrule=0pt, boxsep=0pt, breakable, enlarge bottom by=0.3cm, enhanced jigsaw, borderline west={3pt}{0pt}{tip-green-border}, title={Tip \\[1mm]}, colbacktitle={tip-green-border}, coltitle={tip-green-text}, coltext={tip-green-text}, fonttitle={\small\bfseries}, attach title to upper={}]}
        {\endtcolorbox}
    \newenvironment{codeheading}[1]
        {\tcolorbox[colback=code-filename-background, every box, sharp corners=south, enlarge bottom by=-2.5mm, boxrule=0pt, boxsep=0pt, breakable, enhanced jigsaw, title={#1}, colbacktitle={black}, coltitle={white}, fonttitle={\small\bfseries}, attach title to upper={}]}
        {\endtcolorbox}
     \newcommand{\codesnippet}[1]{\singlespacing \small \begin{codeheading}{#1} \end{codeheading}} 
---

# Usage
This is dummy text

\codesnippet{App.js}
console.log('test');

\bigbreak

\begin{tip}
To be or not to be, that is the question!
\end{tip}

# Heading 2

Some more text...

\codesnippet{Latex.js}
console.log('Hello!')

在此处输入图片描述

答案1

如果您使用\newtcolorbox为此目的而制作的,则不会出现此问题。(您也可以与团体合作,但我更喜欢内置解决方案。)

\documentclass{article}
\usepackage{setspace}
\spacing{1.25}
\usepackage{fancyhdr}
\renewcommand{\headrulewidth}{0pt}
\pagestyle{fancy}
\fancyhead[CO,CE]{This is fancy}
\fancyhead[LE,RO]{\thepage}
\fancyfoot[C]{Blah blah blah}
\usepackage[most]{tcolorbox}
\definecolor{tip-green-text}{HTML}{165c26}
\definecolor{tip-green-background}{HTML}{dcffe4}
\definecolor{tip-green-border}{HTML}{96f1b5}
\definecolor{code-filename-background}{HTML}{161621}
\newtcolorbox{tip}{colback=tip-green-background, 
frame hidden, boxrule=0pt, boxsep=0pt, breakable, 
enlarge bottom by=0.3cm, enhanced jigsaw, 
borderline west={3pt}{0pt}{tip-green-border}, 
title={Tip \\[1mm]}, colbacktitle={tip-green-border}, 
coltitle={tip-green-text}, coltext={tip-green-text}, 
fonttitle={\small\bfseries}, attach title to upper={}}
\newtcolorbox{codeheading}[1]{colback=code-filename-background, every box, sharp corners=south, enlarge bottom by=-2.5mm, boxrule=0pt, boxsep=0pt, breakable, enhanced jigsaw, title={#1}, colbacktitle={black}, coltitle={white},
fonttitle={\small\bfseries}, attach title to upper={}}
\newcommand{\codesnippet}[1]{\singlespacing \small \begin{codeheading}{#1} 
\end{codeheading}} 
\begin{document}
\# Usage
This is dummy text

\codesnippet{App.js}
console.log('test');

\bigbreak

\begin{tip}
To be or not to be, that is the question!
\end{tip}

\# Heading 2

Some more text\dots

\codesnippet{Latex.js}
console.log('Hello!')
\end{document}

在此处输入图片描述

请注意,您的命令目前的形式\codesnippet没有太多意义。下面的命令可能更有用。

\documentclass{article}
\usepackage{setspace}
\spacing{1.25}
\usepackage{fancyhdr}
\renewcommand{\headrulewidth}{0pt}
\pagestyle{fancy}
\fancyhead[CO,CE]{This is fancy}
\fancyhead[LE,RO]{\thepage}
\fancyfoot[C]{Blah blah blah}
\usepackage[most]{tcolorbox}
\definecolor{tip-green-text}{HTML}{165c26}
\definecolor{tip-green-background}{HTML}{dcffe4}
\definecolor{tip-green-border}{HTML}{96f1b5}
\definecolor{code-filename-background}{HTML}{161621}
\newtcolorbox{tip}{colback=tip-green-background, 
frame hidden, boxrule=0pt, boxsep=0pt, breakable, 
enlarge bottom by=0.3cm, enhanced jigsaw, 
borderline west={3pt}{0pt}{tip-green-border}, 
title={Tip \\[1mm]}, colbacktitle={tip-green-border}, 
coltitle={tip-green-text}, coltext={tip-green-text}, 
fonttitle={\small\bfseries}, attach title to upper={}}
\newtcolorbox{codeheading}[1]{colback=code-filename-background, 
every box, sharp corners=south, enlarge bottom by=-2.5mm,
boxrule=0pt, boxsep=0pt, breakable, enhanced jigsaw, 
 coltext={tip-green-text}, 
title={#1\\[1mm]}, colbacktitle={black}, coltitle={white}, 
fonttitle={\small\bfseries}, attach title to upper={}}
\newcommand{\codesnippet}[1]{\singlespacing \small \begin{codeheading}{#1} 
\end{codeheading}} 
\begin{document}
\# Usage
This is dummy text

\begin{codeheading}{App.js}
console.log('test');
\end{codeheading}

\bigbreak

\begin{tip}
To be or not to be, that is the question!
\end{tip}

\# Heading 2

Some more text\dots

\begin{codeheading}{Latex.js}
console.log('Hello!')
\end{codeheading}

\end{document}

在此处输入图片描述

还请注意tcolorbox支持列表。

相关内容