# Draw a box around selected entries of array

I want to draw a dashed-line box as show in picture. Can someone help? Here is the latex code I used for that array:

\begin{array}{ccccccccccccccc}
c^{(1)} & \geq & c^{(2)} & \geq & \cdots & \geq & c^{(j)} & \geq & \cdots & \geq & c^{((N-1)K+1)} & \geq & \cdots & \geq & c^{(NK)}\\
\downarrow &  & \downarrow &  &  &  & \downarrow &  &  &  & \downarrow & & & & \downarrow \\
\gamma^{(1)} &  & \gamma^{(2)} &  & \cdots &  & \gamma^{(j)} &  & \cdots &  & \gamma^{((N-1)K+1)}& &\cdots &  & \gamma^{(NK)}
\end{array}

• I don't believe that is all the code you used to create the array. With neither a class nor a document environment, that will surely give nothing but errors. Look at tikzmark, potentially, but probably you might as well use a matrix of math nodes in that case. – cfr Jun 26 '17 at 2:08
• Given code is only for the array, but not for the rectangle box which I included manually (with Paint). – Frey Jun 26 '17 at 2:31
• But you must have a complete document to produce just the array. Complete examples are much more useful than code fragments .... – cfr Jun 26 '17 at 11:11

With a simple array environment:

\documentclass[12pt]{article}
\usepackage{array,amsmath}
\begin{document}

$\arraycolsep=2pt \begin{array}{ | *7c | *8c }\cline{1-7} \rule{0pt}{3ex}% more vertical space for 1st line c^{(1)} & \geq & c^{(2)} & \geq & \cdots & \geq & c^{(j)} & \geq & \cdots & \geq & c^{((N-1)K+1)} & \geq & \cdots & \geq & c^{(NK)}\\ \downarrow & & \downarrow & & & & \downarrow & & & & \downarrow & & & & \downarrow \\ \gamma^{(1)} & & \gamma^{(2)} & & \cdots & & \gamma^{(j)} & & \cdots & & \gamma^{((N-1)K+1)}& &\cdots & & \gamma^{(NK)}\\\cline{1-7} \end{array}$

\end{document}


• I like this code with no special packages. I use : instead | and \cdashline instead \cline to make the rectangle with dashed-line. Thanks @Herbert – Frey Jun 26 '17 at 23:16

With a tikz matrix:

\documentclass[12pt]{article}
\usepackage{array,amsmath}
\usepackage{tikz}
\usetikzlibrary{matrix,fit}
\begin{document}
\begin{center}
\begin{tikzpicture}
\matrix[matrix of math nodes](m){%
c^{(1)} & \geq & c^{(2)} & \geq & \cdots & \geq & c^{(j)} &[8pt] \geq & \cdots & \geq & c^{((N-1)K+1)} & \geq & \cdots & \geq & c^{(NK)}\\
\downarrow &  & \downarrow &  &  &  & \downarrow &  &  &  & \downarrow & & & & \downarrow \\
\gamma^{(1)} &  & \gamma^{(2)} &  & \cdots &  & \gamma^{(j)} &  & \cdots &  & \gamma^{((N-1)K+1)}& &\cdots &  & \gamma^{(NK)}\\
};
\node[fit=(m-1-1)(m-3-7), draw, thick] {};
\end{tikzpicture}
\end{center}
\end{document}


Using tcolorbox package

   \documentclass[10.0pt,a4paper]{article}
\usepackage[all]{tcolorbox}
\usepackage[english]{babel}

\begin{document}
\begin{minipage}{5mm}
\begin{tcolorbox}[enhanced,arc=0mm,boxrule=1.5mm,frame hidden,colback=green!8!white,borderline={1mm}{0mm}{black,dotted},text width=5cm]

$\begin{array}{ccccccc} c^{(1)} & \geq & c^{(2)} & \geq & \cdots & \geq & c^{(j)}\\ %& \geq & \cdots & \geq & c^{((N-1)K+1)} & \geq & \cdots & \geq & c^{(NK)}\\ \downarrow & & \downarrow & & & & \downarrow \\ %\downarrow & & & & \downarrow & & & & \downarrow &\\ \gamma^{(1)} & & \gamma^{(2)} & & \cdots & & \gamma^{(j)}\\ %& & \cdots & & \gamma^{((N-1)K+1)}& &\cdots & & \gamma^{(NK)} \end{array}$
\end{tcolorbox}
\end{minipage}\hfill\hfill\hfill
\raisebox{0.5ex}{\begin{minipage}{5mm}
$\begin{array}{ccccccccc} \geq & \cdots & \geq & c^{((N-1)K+1)} & \geq & \cdots & \geq & c^{(NK)}\\ & & & \downarrow & & & & \downarrow \\ & \cdots & & \gamma^{((N-1)K+1)}& &\cdots & & \gamma^{(NK)} \end{array}$
\end{minipage}}\hfill\hfill~

\end{document}


Using TikZ Matrix nodes

    \documentclass[10pt,a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{center}
\begin{tikzpicture}
\matrix [column sep=1mm]
{
\node (a) {};& &  &  &  &  & &  &  \node (b) {}; &  &  &   & & & &  \\
&\node {$c^{(1)}$}; & \node {$\geq$}; & \node {$c^{(2)}$}; & \node {$\geq$}; & \node {$\cdots$}; & \node {$\geq$}; & \node {$c^{(j)}$}; & &\node {}; \node {$\geq$}; & \node {$\cdots$}; & \node {$\geq$}; & \node {$c^{((N-1)K+1)}$}; & \node {$\geq$}; & \node {$\cdots$}; & \node {$\geq$}; & \node {$c^{(NK)}$};\\
&\node {$\downarrow$}; &  & \node {$\downarrow$}; &  &  &  & \node {$\downarrow$}; &  & &  &  & \node {$\downarrow$}; & & & & \node {$\downarrow$}; \\
&\node {$\gamma^{(1)}$}; &  & \node {$\gamma^{(2)}$}; &  & \node {$\cdots$}; &  & \node {$\gamma^{(j)}$}; &\node {};&  & \node {$\cdots$}; &  & \node {$\gamma^{((N-1)K+1)}$}; &  & \node {$\cdots$}; &  & \node {$\gamma^{(NK)}$};\\
\node (c) {};& &  &  &  &  &  &  & \node (d) {}; &  &  &   & & & &  \\
};
\draw [black,thick,dashed] (a.east) -- (c.east) {};
\draw [black,thick,dashed] (d.west) -- (b.west) {};
\draw [black,thick,dashed] (a.east) -- (b.west) {};
\draw [black,thick,dashed] (c.east) -- (d.west) {};
\end{tikzpicture}

\end{center}
\end{document}


• Nice, but the row parts don't seem aligned. – egreg Jun 26 '17 at 7:55
• @egreg Do they look better now? – cfr Jun 26 '17 at 11:10
• The spacing between the columns in the tcolorbox example is uneven. – TH. Jun 26 '17 at 20:36

A very short code with pstricks and auto-pst-pdf (to compile with pdflatex): it uses an ordinary matrix, inserted in a postscript environment, adds two empty nodes for the ends of a diagonal of the box and draws the \psframe defined by these nodes:

    \documentclass[12pt, svgnames]{article}
\usepackage{array,amsmath}
\usepackage{pst-node, auto-pst-pdf}

\setcounter{MaxMatrixCols}{20}
\begin{document}

$\begin{postscript} \begin{matrix} \pnode[-1ex, 2.8ex]{A}c^{(1)} & \geq & c^{(2)} & \geq & \cdots & \geq & c^{(j)}& \geq & \cdots & \geq & c^{((N-1)K+1)} & \geq & \cdots & \geq & c^{(NK)}\\ \downarrow & & \downarrow & & & & \downarrow & & & & \downarrow & & & & \downarrow \\ \gamma^{(1)} & & \gamma^{(2)} & & \cdots & & \gamma^{(j)}\pnode[1ex, -1ex]{B} & & \cdots & & \gamma^{((N-1)K+1)}& &\cdots & & \gamma^{(NK)} \end{matrix} \psframe[linecolor=IndianRed](A)(B) \end{postscript}$

\end{document}