1

I'd like to define a tcolorbox environment, e.g.,

\newtcolorbox{mybox}{
enhanced,
boxrule=0pt,frame hidden,
borderline west={3pt}{0pt}{Peach},
colback=Peach!5,
sharp corners
}

But I would like for each box of this kind to begin with a certain keyword, e.g., "Example" – not as a title, just as a part of the body. Is there a way to build this in to the box definition?

Thanks.

2
  • Try with before upper=Example
    – Ignasi
    Apr 1, 2022 at 15:16
  • @Ignasi Thanks! Do you know how I can apply a certain font/formatting to just the text in before upper? Without extending to the remainder of the body?
    – ChasingUG
    Apr 1, 2022 at 16:24

2 Answers 2

3

As an alternative to my comment, it's also possible to use attach title to upper which includes the title in upper part. In this case, the font and color can be selected with fonttitle or coltitle options.

\documentclass[dvipsnames]{article}
\usepackage[most]{tcolorbox}

\newtcolorbox{mybox}[1][]{
enhanced,
boxrule=0pt,frame hidden,
borderline west={3pt}{0pt}{Peach},
colback=Peach!5,
title=Example,
coltitle=Peach,
attach title to upper={\ },
fonttitle=\bfseries,
sharp corners,
#1
}

\begin{document}
\begin{mybox}
This is an example
\end{mybox}

\begin{mybox}[coltitle=RubineRed, title=Question]
This is a question
\end{mybox}

\end{document}

enter image description here

1

@Ignasi's comment already solved the main question. This answer provides how to use user defined fonts and also a tip about how to create new tcolorboxes a little more flexible.

A very basic list of fonts may be found in this tutorial from Overleaf. Please notice you may have to find a package for this or that specific font, in my MWE, I used consolas through:

\usepackage{inconsolata}
\newcommand{\myfont}{\fontfamily{zi4}\selectfont\small}

Reading the tcolorbox manual you may notice they commonly define new objects with an argument -- normally optional -- at the end of the options block. For example, in page 15, there is \newtcolorbox{mybox}[2][]{..., which is a two argument function, where the first argument is optional. The last line reads ...title={#2},#1}. This #1 is the optional argument, it is placed at the end you may add or overwrite any previously defined set.

At a first glance, this option may seem useless, as you have already defined your style, why bothering with local options? Well, because sometimes you may have to change something locally without creating a whole new kind of box.

OBS: You may also define some \tcbset and create a new one based on a previous one, but that a skill for another answer.

A MWE follows:

\documentclass{article}

\usepackage{inconsolata}
\newcommand{\myfont}{\fontfamily{zi4}\selectfont\small}

\usepackage[skins]{tcolorbox}
\newtcolorbox{mybox}[1][]{
  enhanced,
  boxrule=0pt, frame hidden,
  borderline west={3pt}{0pt}{blue},
  colback=blue!5,
  sharp corners,
  before upper={\textbf{\myfont{Example:} }},
  #1
}

\begin{document}
\begin{mybox}
  This is my box.
\end{mybox}
\begin{mybox}[colbacktitle=purple, title={This is mybox with option, the option is a title}]
This is my box.
\end{mybox}
\begin{mybox}[colback=red!20, borderline east={3pt}{0pt}{yellow}]
This is my box.
\end{mybox}
\end{document}

enter image description here

1
  • 1
    Thanks for this. It's very helpful, and I'll definitely add arg structure to my work.
    – ChasingUG
    Apr 2, 2022 at 19:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .