# How to write mathematics with boxes?

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

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?

• Before asking a new question, if you think the answers useful (incl. answers to your past questions), then consider accepting it by clicking the tick mark below the voting mark. Feb 2, 2019 at 8:37

You can use \boxed command from amsmath package

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

\begin{equation*}
\end{equation*}

\end{document}


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*}
\end{equation*}

\end{document}


• I would like to change the color of each box
– user132638
Jan 31, 2019 at 7:46
• One problem is I have added three symbol in the second box. It is getting bigger. Is it possible to fix the size of each of the box irrespective of the text entered.
– user132638
Jan 31, 2019 at 7:49
• I think you misunderstood me, I mean to say in the second box put three symbols and then see
– user132638
Jan 31, 2019 at 7:52
• Lets say I want five symbol maximum in the box
– user132638
Jan 31, 2019 at 7:56
• @Isla_Bonita So you want all the three boxes with the same size irrespective of their contents? Jan 31, 2019 at 7:59

Depends on how fancy you want to go. In principle you do not need any packages for that.

\documentclass{article}
\begin{document}
$X= \raisebox{-1.5ex}{\begin{array}{c} \hline \multicolumn{1}{|c|}{X_1 X_2}\\ \hline \leftarrow\ell\rightarrow\\ \end{array}}~ \raisebox{-1.5ex}{\begin{array}{c} \hline \multicolumn{1}{|c|}{X_3 X_4}\\ \hline \leftarrow\ell\rightarrow\\ \end{array}}\cdots \raisebox{-1.5ex}{\begin{array}{c} \hline \multicolumn{1}{|c|}{X_5 X_6}\\ \hline \leftarrow\ell\rightarrow\\ \end{array}}$

\end{document}


Here's a package-free version (amsmath is always needed if one does math).

\documentclass{article}
\usepackage{amsmath}

\newcommand{\block}[2]{%
\begin{array}[t]{@{}c@{}}
\boxed{\mathstrut #1}\\
\scriptstyle\blockleftarrowfill\,#2\,\blockrightarrowfill
\end{array}%
}
\newcommand{\blockleftarrowfill}{%
\mathord\leftarrow
\mkern -7mu
\cleaders\hbox{$\scriptstyle\mkern -2mu\smash-\mkern -2mu$}\hfill
\mkern -7mu\smash-%
}
\newcommand{\blockrightarrowfill}{%
\smash-%
\mkern -7mu
\cleaders\hbox{$\scriptstyle\mkern -2mu\smash-\mkern -2mu$}\hfill
\mkern -7mu
\mathord\rightarrow
}

\begin{document}

$X= \block{x_1\ x_2\quad}{l}\quad \block{x_3\ x_4\quad}{l}\quad \cdots\quad \block{x_5\ x_6\quad}{l}$

\end{document}