1

I am new to the LaTeX. I want to customize tcolorbox for custom title with auto counter and background colour as per my choice say light red. Also I want to change the font colour of title and inside text contents also as per my choice to say dark red/blue. How can I do this. Please help.

The basic MWE is as provided by feculededentier in the below link page

I have found this page Example numbers with respect to chapters in tcolorbox title

Deb bijan
  • 131
  • Welcome to TeX.SX! In future it is better if you add a minimal working example so your question as this helps to explain what you agree trying to do. Given that you already have two answers I guess it was clear enough in this case:) –  Jul 30 '18 at 05:47

2 Answers2

2

To very first approximation I would just take the answer you refer to, remove the chapter stuff and add the colors.

\documentclass{article}
\usepackage{etoolbox}
\usepackage{tcolorbox}
\tcbuselibrary{skins,breakable}
\usepackage{lipsum}

\tcbset{mytitle/.style={title={Example~\thetcbcounter\ifstrempty{#1}{}{: #1}}}}
\newtcolorbox[auto counter, number within=chapter, 
number freestyle={\noexpand\arabic{\tcbcounter}}]{myexample}[1][]{%
    enhanced,
    breakable,
    fonttitle=\bfseries,
    mytitle={},
    #1
}

\begin{document}


\begin{myexample}[colback=red!5!white,colframe=red!75!black,mytitle={First example}, label=exfirst]
  \lipsum[4]
\end{myexample}

\begin{myexample}[colback=red!5!white,colframe=blue!75!black]
  \lipsum[4]
\end{myexample}

\begin{myexample}[colback=red!5!white,colframe=red!75!black,mytitle={Third example}, label=exthird]
  \lipsum[4]
\end{myexample}

I can now refer to example~\ref{exfirst} and example~\ref{exthird}.
\end{document}

enter image description here

EDIT: One way to deal with the color issue is to introduce new macros.

\documentclass{article}
\usepackage{etoolbox}
\usepackage{tcolorbox}
\tcbuselibrary{skins,breakable}
\usepackage{lipsum}

\tcbset{mytitle/.style={title={Example~\thetcbcounter\ifstrempty{#1}{}{: #1}}}}
\newtcolorbox[auto counter, number within=chapter, 
number freestyle={\noexpand\arabic{\tcbcounter}}]{myexample}[1][]{%
    enhanced,
    breakable,
    fonttitle=\bfseries,
    mytitle={},
    #1
}

\newcommand{\BlueBox}[3][]{
\begin{myexample}[colback=blue!5!white,colframe=blue!75!black,mytitle={#2},#1]
 #3
\end{myexample}
}

\newcommand{\RedBox}[3][]{
\begin{myexample}[colback=blue!5!white,colframe=red!75!black,mytitle={#2},#1]
 #3
\end{myexample}
}


\begin{document}


\RedBox[label=exfirst]{First Example}{\lipsum[1]}

\BlueBox[label=exsecond]{}{\lipsum[2]}

\RedBox[label=exthird]{Third Example}{\lipsum[3]}


I can now refer to example~\ref{exfirst} and example~\ref{exthird}.
\end{document}
  • Thanks @marmot for a solution. But how do I get that colback and colframe formating in the preamble macro so that every time I don't have to type such long customized color format if it happens that I need to change later for all of the examples. – Deb bijan Jul 30 '18 at 13:03
  • @DebBijan I made an update. –  Jul 30 '18 at 14:06
  • Thanks @marmot for the update. But I also need to change the font color of main text inside the box, say red. Using color{red} I only get current page red font, but on multiple pages, it resets to default. How to do it in the above example – Deb bijan Jul 30 '18 at 14:47
  • ok @marmot, I changed the font colour using colupper=red inside the Redbox custom macro. Is it ok to do this way. – Deb bijan Jul 30 '18 at 15:07
  • @DebBijan yes of course. There is also the possibility to define your own colors and use those and to redefine them if needed. –  Jul 30 '18 at 18:19
1

Welcome:)

\documentclass{book}
\usepackage{tcolorbox}
    \tcbuselibrary{skins,breakable}
\usepackage{lipsum}

\newtcolorbox[auto counter, number within=chapter, number freestyle={\noexpand\thechapter.\noexpand\arabic{\tcbcounter}}]{myexample}[2][]{%
    enhanced,
    breakable,
    fonttitle=\bfseries,
    title=\textcolor{red}{Example}~\textcolor{red}{\thetcbcounter:} \textcolor{red}{#2},
    #1
}

\newtcolorbox[auto counter, number within=chapter, number freestyle={\noexpand\thechapter.\noexpand\arabic{\tcbcounter}}]{myexampleblue}[2][]{%
    enhanced,
    breakable,
    fonttitle=\bfseries,
    title=\textcolor{blue}{Example}~\textcolor{blue}{\thetcbcounter:} \textcolor{blue}{#2},
    #1
}

\begin{document}

\chapter{First chapter}

\begin{myexample}[label=exfirst]{First example}
  \lipsum[4]
\end{myexample}

\begin{myexampleblue}{Second example}
  \lipsum[4]
\end{myexampleblue}


I can now refer to example~\ref{exfirst} and example~\ref{exthird}.
\end{document} 

enter image description here

Saravanan
  • 1,475
  • 1
    Thanks, @Saravanan. But I also need to change the background color of the box and also font say I want the background in light red and texts in red inside the box. Also the default grey title box, I need to change it to custom color say blue – Deb bijan Jul 30 '18 at 14:54