3

How can we use tikz package to draw a rectangle and fill it with a color for the equations? like the following

enter image description here

\documentclass{article}
\usepackage{tikz}
\usepackage{amsmath}
\begin{document}
\begin{equation}
f(x)=ax+b
\end{equation}
\end{document}
2

2 Answers 2

8

enter image description here

\documentclass{article}
\usepackage{amsmath}
\usepackage[customcolors]{hf-tikz}

\begin{document}
\hfsetfillcolor{blue!10}
\hfsetbordercolor{blue}
\begin{equation}\label{e:barwq2}
\tikzmarkin{b}  f(x)=ax+b\tikzmarkend{b}\\
\end{equation}
\end{document}

for white borders

enter image description here

\documentclass{article}
\usepackage{amsmath}
\usepackage[customcolors]{hf-tikz}

\begin{document}
\hfsetfillcolor{blue!20}
\hfsetbordercolor{white}
\begin{equation}\label{e:barwq2}
\tikzmarkin{b}  f(x)=ax+b\tikzmarkend{b}\\
\end{equation}
\end{document}
1
  • Thank you so much.
    – Momo
    Commented Feb 9, 2021 at 6:31
5

You want to use tcolorbox:

\documentclass{article}
\usepackage{amssymb}
\usepackage{tcolorbox}

\begin{document}

    \begin{tcolorbox}[colback=blue!10,text width=5cm]
\begin{equation}
\centering  f(x)=ax+b
\end{equation}
    \end{tcolorbox}
\end{document}

enter image description here

with \tcbset{colframe=white}:

enter image description here

in tikz(but tcolorobx i think is the better one):

\documentclass{article}
\usepackage{amssymb}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}

\node [fill=cyan!20,text width=4cm] at (0,0) {\begin{equation}f(x)=ax+b\end{equation}};

    \end{tikzpicture}
\end{document}

enter image description here

8
  • Thank you so much.
    – Momo
    Commented Feb 9, 2021 at 5:36
  • How can we do it without frame?
    – Momo
    Commented Feb 9, 2021 at 5:38
  • How can we do it with \usepackage{tikz}? because I'd like to use its color.
    – Momo
    Commented Feb 9, 2021 at 5:40
  • without frame use \tcbset{colframe=white}
    – Roland
    Commented Feb 9, 2021 at 5:57
  • you can also use : \begin{tcolorbox}[enhanced,colback=blue!10,text width=5cm, frame hidden] with \tcbuselibrary{skins} in the prehambule
    – flav
    Commented Feb 9, 2021 at 6:05

You must log in to answer this question.

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