[Tex/LaTex] How to write mathematics with boxes

boxes

I want to write the mathematics inside the box as written below.

enter image description here

See my code given below:

\documentclass{article}

\begin{document}
\\fbox{text}
\framebox[width][pos]{text}

\makebox[\textwidth]{X_1 X_2} \par
\makebox[\textwidth]{X_3 X_4} \par
\cdots
\makebox[\textwidth]{X_4 X_6} \par

\end{document}

Question: I want to write the above maths, How to write it properly?

Best Answer

You can use \boxed command from amsmath package

\documentclass{article}
\usepackage{amsmath}
\begin{document}

\begin{equation*}
    X=\boxed{X_1 X_2}\quad \boxed{X_3 X_4}\cdots\boxed{X_5 X_6}
\end{equation*}



\end{document}

enter image description here

To add colors and labels below boxes

\documentclass{article}
\usepackage{amsmath,tcolorbox}
\tcbuselibrary{theorems,skins}
\usetikzlibrary{arrows.meta}

\newlength\mylabelshift
\setlength\mylabelshift{3mm} % to be adjusted if needed

\newcommand{\drawlabel}[1]{\draw[Stealth-Stealth]([yshift=-\mylabelshift]frame.south west)--node[fill=white]{\(#1\)}([yshift=-\mylabelshift]frame.south east);}
\newtcbox{\mymathbox}[1][l]{
               enhanced,
               nobeforeafter,
               math upper,
               tcbox raise base,
               colback=blue!30, % to set background color
               size=small,
               geometry nodes,
               overlay={\drawlabel{#1}},
               tcbox width=minimum center,
               width= 2 cm ,% to be adjusted if needed
               }
\begin{document}

\begin{equation*}
    X=\mymathbox{X_1 X_2}\quad \mymathbox{X_3 X_4 X_5}\cdots\mymathbox{X_5 X_6}
\end{equation*}
\vspace{1cm}
\begin{equation*}
    X=\mymathbox[a]{X_1 X_2}\quad \mymathbox[b]{X_3 X_4}\cdots\mymathbox[c]{X_5 X_6}
\end{equation*}


\end{document}

enter image description here

Related Question