8

Since in my document I have to write a lot of math in colored box, I'd like to avoid to write $ inside every box. Is there a way to define newtcbox in math mode?

I tried with \newtcbox{\mywbox}[1]{<options>}{$\displaystyle #1$} but it doesn't work.

This is my latex code:

\usepackage{tcolorbox} \newtcbox{\mywbox}{on line,colback=white,colframe=black,size=fbox,arc=3pt,boxrule=0.8pt} \begin{document} \obeylines This \mywbox{$3x$} is in line math. This $$y=\mywbox{$-5x$}-5+6$$ is not in line math. \end{document} 

enter image description here

I read about \tcboxmath and \tcbhighmath in the official tcolorbox manual, but I don't understand how to define them simply as the newtcbox and if they are what I need.

2 Answers 2

7

I would say

\newtcbox{\mywboxtext}{on line,colback=white,colframe=black,size=fbox,arc=3pt,boxrule=0.8pt} \newcommand{\mywboxmath}[1]{\mywboxtext{$#1$}} 

Your example can become

\documentclass{article} \usepackage{tcolorbox} \newtcbox{\mywboxtext}{on line,colback=white,colframe=black,size=fbox,arc=3pt,boxrule=0.8pt} \newcommand{\mywboxmath}[1]{\mywboxtext{$#1$}} \begin{document} This \mywboxmath{3x} is in line math and this \[ y=\mywboxmath{-5x}-5+6 \] is display math. \end{document} 

enter image description here

Change the names to your liking.

Avoid $$ in LaTeX (and of course also \obeylines).

Should you need the boxes to behave in subscripts or superscripts, change the code into

\documentclass{article} \usepackage{tcolorbox} \newtcbox{\mywboxtext}{on line,colback=white,colframe=black,size=fbox,arc=3pt,boxrule=0.8pt} \makeatletter \newcommand{\mywboxmath}[1]{\mathpalette\mywboxmath@do{#1}} \newcommand{\mywboxmath@do}[2]{\mywboxtext{$\m@th#1#2$}} \makeatother \begin{document} This $\mywboxmath{3x}$ is in line math and this \[ y_{\mywboxmath{0}}=\mywboxmath{-5x}-5+6 \] is display math. \end{document} 

Note that in this case you need that \mywboxmath is in math mode.

0
13

You can do this using \tcboxmath from the theorems library of tcolorbox:

\documentclass{article} \usepackage{tcolorbox} \tcbuselibrary{theorems} \newcommand*{\mywbox}{% \tcboxmath[colback=white, colframe=black, size=fbox, arc=3pt, boxrule=0.8pt]% } \begin{document} This \mywbox{3x} is in line math. This \[ y = \mywbox{-5x} - 5 + 6 \] is not in line math. \end{document} 

Screenshot

You may want to abstract things a little bit using a style too, so that you can apply it to other boxes should the need arise:

\documentclass{article} \usepackage{tcolorbox} \tcbuselibrary{theorems} \tcbset{my math box/.style={ colback=white, colframe=black, size=fbox, arc=3pt, boxrule=0.8pt} } \newcommand*{\mywbox}{\tcboxmath[my math box]} \begin{document} This \mywbox{3x} is in line math. This \[ y = \mywbox{-5x} - 5 + 6 \] is not in line math. \end{document} 

Making the background of the box transparent

This can be done using /tcb/opacityback and a skin using a jigsaw frame engine, such as standard jigsaw or enhanced jigsaw.

\documentclass[fleqn]{article} \usepackage{tcolorbox} \tcbuselibrary{theorems} \tcbset{ my math box/.style={ standard jigsaw, % useful for 'opacityback' and 'opacityframe' colback=green!20, colframe=black, size=fbox, arc=3pt, boxrule=0.8pt, opacityback=0.6, } } \newcommand*{\mywbox}{\tcboxmath[my math box]} \begin{document} This \makebox[0pt]{% \raisebox{-0.5\height}[0pt][0pt]{\hspace{1cm}\includegraphics{example-image-duck}}% }% \mywbox{3x} is in line math. This \[ y = \mywbox{-5x} - 5 + 6 \] is not in line math. \end{document} 

enter image description here

2
  • Is there a way to make the back of the layer/box transparent respectively lower the opacity? Commented Apr 12, 2021 at 19:54
  • 1
    @Leon I've added an example of this to my answer. Commented Apr 12, 2021 at 20:50

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.