tables boxes add tag
nsajko
I want to reproduce this in LaTeX, preferably using nicematrix:

![synthetic division table.webp](/image?hash=d15d3880d38e693d6e401b043a7e286fbeba0f3cf0540bea04b3b7268b0aae1f)

The problematic parts for me are:

1. the partial box around the first element
2. the full box around the last element

I think I could solve this using a one-element-table for each of the two boxes, but I'm wondering whether this is a good approach, and what approach would you people here recommend.
Top Answer
Skillmon
An old-school solution without `nicematrix` (sorry, but I haven't yet taken the time to learn that package, and I have yet to want to create a table I'm not able to create with only `array`+`longtable`+`tabularx`). It lacks the grey base colour but else should do.

I'd use `\fbox` for the full box, and though there is likely a package that provides partial boxes I had some fun and created a `\partialfbox` macro that as a first argument expects a string specifying the sides a rule should be drawn on (`b`, `r`, `t`, and `l`). It's using the same code an `\fbox` uses, just adding a few conditionals for the rules (it even uses an internal for the drawing part as `\fbox` does):

```
\documentclass{article}

\usepackage{color}
\usepackage{array}
\setlength\extrarowheight{3pt}

\makeatletter
\newcommand\partialfbox@check[2]
  {\in@{#1}{#2}\expandafter\let\csname ifpartialfbox@#1\endcsname\ifin@}
\newcommand*\partialfbox[2]{}% just to check the name
\protected\def\partialfbox#1#2%
  {%
    \begingroup
      \leavevmode
      \partialfbox@check{b}{#1}%
      \partialfbox@check{r}{#1}%
      \partialfbox@check{t}{#1}%
      \partialfbox@check{l}{#1}%
      \setbox\@tempboxa\hbox
        {%
          \color@begingroup
          \kern\fboxsep
          {#2}%
          \kern\fboxsep
          \color@endgroup
        }%
      \@partialframeb@x\relax
  }
\newcommand*\@partialframeb@x[1]
  {%
      \ifpartialfbox@b
        \@tempdima=\fboxrule
      \else
        \@tempdima=\z@
      \fi
      \advance\@tempdima\fboxsep
      \advance\@tempdima\dp\@tempboxa
      \hbox
        {%
          \lower\@tempdima\hbox
            {%
              \vbox
                {%
                  \ifpartialfbox@t\hrule\@height\fboxrule\fi
                  \hbox
                    {%
                      \ifpartialfbox@l\vrule\@width\fboxrule#1\fi
                      \vbox
                        {%
                          \vskip\fboxsep
                          \box\@tempboxa
                          \vskip\fboxsep
                        }%
                      \ifpartialfbox@r#1\vrule\@width\fboxrule\fi
                    }%
                  \ifpartialfbox@b\hrule\@height\fboxrule\fi
                }%
            }%
        }%
    \endgroup
  }
\makeatother

\begin{document}
$\begin{array}{*5r}
  \partialfbox{rb}{$\mathcolor{blue}{3}$}
    & 2 & -5 & -7 & \mathcolor{magenta}{7} \\
    &   &  6 &  3 & \mathcolor{green}{-12} \\
  \hline
    & 2 &  1 & \mathcolor{red}{-4} & \fbox{$\mathcolor{blue}{-1}$} \\
\end{array}$
\end{document}
```

![reproducetable-1.png](/image?hash=47a70321f9762dafb2ce09f6a8eb8e5dea162bc7f4c6b15a84857c43bc237f5a)
Answer #2
samcarter
The nice thing about nicematrix is that every cell automatically creates a tikz node. This means you can go in with tikz afterwards and add whatever decoration you might want.

Just some possible examples:


```
\documentclass{article}

\usepackage{nicematrix}
\usepackage{tikz}

\tikzset{foo/.style={
  draw,
  thick,
  rectangle,
  inner sep=1pt,
}}

\begin{document}
\[
\begin{NiceMatrix}
1 & 2 & 3 \\
4 & 5 & 6 \\
\CodeAfter
\tikz \node[thick,draw,rectangle,minimum size=0.5cm] at (2-3) {};
\tikz \draw[thick] (1-1) ++(-0.25cm,-0.25cm) -- ++(0.5cm,0) -- ++(0,0.5cm);
\end{NiceMatrix}
\]
\end{document}
```

![Screenshot 2023-02-11 at 23.35.59.png](/image?hash=090fdb712df24c01087005850f857df76a0c1bf6124f8b6222bc4ccd0fe842c5)

Enter question or answer id or url (and optionally further answer ids/urls from the same question) from

Separate each id/url with a space. No need to list your own answers; they will be imported automatically.