2

I have two environments, tip, and codeheading, each with their own tcolorboxes, like so:

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

I'm noticing that the tip environment tcolorbox is picking up stylistic values that have been defined in the codeheading environment.

For example, codeheading has sharp corners=south, which tip then also has. If I change this value from south to north, tip's corners also change.

Why would two tcolorboxes be picking up each other's style values? It's like after they colorboxes are used on the page, each one starts picking up the other's style. They get..confused?

I'm using Pandoc, too.

---
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!')

enter image description here

  • 1
    Welcome to TeX-SE! Please note that tcolorbox has a command\newtcolorbox specifically for that. And please provide us with a complete code that starts with \documentclass and ends with \end{document} and can be compiled. – user121799 Jul 5 at 10:25
  • 1
    make a complete example for tests. – Ulrike Fischer Jul 5 at 10:29
  • @marmot I've edited my answer to include the entire code snippet and updated the screenshots to show the output. – jcrowson Jul 5 at 10:36
  • a code snippet is not a complete example. – Ulrike Fischer Jul 5 at 12:27
2

If you use \newtcolorbox, which is made for this, the issue does not arise. (You could also work with groups but I would prefer the built-in solution.)

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

enter image description here

Notice that in its present form your command \codesnippet does not make too much sense. Something like the following may be more useful.

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

enter image description here

Notice also that tcolorbox supports listings.

  • 1
    +1. Also notice that you can factor out common styles with \tcbset{common/.style={boxrule=0pt, boxsep=0pt, breakable,},} and then use it as \newtcolorbox{tip}{common, ... – Rmano Jul 5 at 11:10
  • I'm not able to compile this successfully. I'm using pandoc and I get the following error: ! LaTeX Error: Missing \begin{document}. I've debugged it and I only get this error when using a newtcolorbox with parameters. – jcrowson Jul 5 at 11:27
  • @Rmano Thanks!! – user121799 Jul 5 at 11:34
  • 1
    @jcrowson Frankly, I doubt it, but I also do not use pandoc. You can use your approach and then add \begingroup and \endgroup, ` \newenvironment{tip} {\begingroup\tcolorbox[....}{\endtcolorbox\endgroup}`. And in future add such relevant information to your questions, otherwise you frustrate everyone. – user121799 Jul 5 at 11:38
  • @marmot I tried the \begingroup and \endgroup syntax and that worked. There must be a bug in Pandoc's parser. Thanks! – jcrowson Jul 5 at 11:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.