0

I am very new to LaTeX. I needed customized colourbox and got this excellent customization provided by @Gonzalo Medina . I have got this from here Create a color box? and just copy pasting the program

I need to have label for the example and also assumption macros for referencing. Also, I found that when colorbox runs over multiple pages, the body text font colour (say I want red) changes to default black. How to correct this. I have used \color{red} \Blindtext. Is it correct way to change text font colour. I found that it changes font colour on the same page, but for multipage colorbox, the text color resets to black.

\documentclass{article}
\usepackage[many]{tcolorbox}
\usepackage[many]{tcolorbox}
\definecolor{myblue}{RGB}{0,163,243}
\tcbset{mystyle/.style={
  breakable,
enhanced,
  outer arc=0pt,
  arc=0pt,
  colframe=myblue,
  colback=myblue!20,
  attach boxed title to top left,
  boxed title style={
    colback=myblue,
    outer arc=0pt,
    arc=0pt,
    },
  title=Example~\thetcbcounter,
  fonttitle=\sffamily
  }
}

\newtcolorbox[auto counter,number within=section]{example}[1][]{
  mystyle,
  title=Example~\thetcbcounter,
  }
\newtcolorbox[auto counter]{assumption}[1][]{
  mystyle,
  colback=white,
  rightrule=0pt,
  toprule=0pt,
  title=Assumption SLR.\thetcbcounter,
  }

\begin{document}

\section{Test section}
\begin{example}
\color{red} \Blindtext % Is this correct way to change font color??
\end{example}
\begin{assumption}
\color{red} \Blindtext  % Is this correct way to change font color?
\end{assumption}

\end{document}
1

Add a label option to every box for referencing. In following code I've done it adding a second mandatory parameter to example and assumption definitions. The optional parameter can be used to change default settings.

About color problem can be solved using the colupper option which fixes text color. Command \color{red} should not be used in this case.

\documentclass{article}
\usepackage[many]{tcolorbox}
\usepackage{blindtext}

\definecolor{myblue}{RGB}{0,163,243}
\tcbset{
mystyle/.style={
    breakable,
    enhanced,
    outer arc=0pt,
    arc=0pt,
    colframe=myblue,
    colback=myblue!20,
    attach boxed title to top left,
    boxed title style={
        colback=myblue,
        outer arc=0pt,
        arc=0pt,
   },
    title=Example~\thetcbcounter,
    fonttitle=\sffamily,
    colupper=red,
    }
}

\newtcolorbox[auto counter,number within=section]{example}[2][]{
  mystyle,
  title=Example~\thetcbcounter,
  label=#2,
  #1
  }

\newtcolorbox[auto counter]{assumption}[2][]{
  mystyle,
  colback=white,
  rightrule=0pt,
  toprule=0pt,
  title=Assumption SLR.\thetcbcounter,
  label=#2,
  #1
  }

\begin{document}

\section{Test section}
\begin{example}{A}
\Blindtext % Is this correct way to change font color??
\end{example}

As you can see in \ref{A} \dots

\begin{assumption}[colupper=myblue!50!red]{B}
\Blindtext  % Is this correct way to change font color?
\end{assumption}

\end{document}

enter image description here

  • Thanks @Ignasi. But how can I add optional title after thetcbcounter. Actually I want to add the current section title after counter e.g. Example 1.1 Memory Devices in the heading of colorbox – Deb bijan Jul 30 '18 at 14:19
  • @DebBijan What you want to do would be easier with a \newtcbtheorem box. You can find an example in updated section of this answer. And better information in corresponding section of tcolorbox documentation – Ignasi Jul 30 '18 at 16:11

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.