# How to put a background letter in a table?

I have a table with some numbers in it, such as

\documentclass{article}

\begin{document}

\begin{tabular}{llll}
1 & 2 & 3 & 4 \\
1 & 2 & 3 & 4 \\
1 & 2 & 3 & 4 \\
1 & 2 & 3 & 4 \\
1 & 2 & 3 & 4
\end{tabular}

\end{document}


I would like to have a "background" for the table (I actually want to be able to specify it for specific blocks in the table) -- the background should be an alphabet letter (such as A) in light shaded gray that doesn't hide the numbers in black.

How would it be possible to do that?

-
Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. – Martin Schröder Jan 17 at 21:27
What do you mean by a 'block' exactly? – cfr Jan 17 at 21:33
@cfr so for example, for the first two lines 1-2 there will be one letter, and for the second and third lines 1-2 there will be a different letter, for the 3-4 a different letter, etc. – tabular Jan 17 at 21:48
– cmhughes Jan 18 at 11:31

Without absolute positioning using tikzmark library for the tikz package.

\documentclass{article}
\usepackage{tikz, lmodern}
\usetikzlibrary{calc,tikzmark}
\begin{document}

\begin{tikzpicture}[overlay,remember picture]
\node [yshift=.6ex] at ( $(pic cs:A) !.5! (pic cs:B)$ ){ \fontsize{60}{60}\selectfont\textbf{\color{gray!20}A} };
\end{tikzpicture}

\begin{tabular}{llll}
\tikzmark{A}%
1 & 2 & 3 & 4 \\
1 & 2 & 3 & 4 \\
1 & 2 & 3 & 4 \\
1 & 2 & 3 & 4 \\
1 & 2 & 3 & 4\tikzmark{B}%
\end{tabular}

\end{document}


Observe that the tikzpicture is drawn here before the table text is typeset and before its coordinates are defined. The purpose here is to let the text go on top of the picture. This will work in most cases, because the coordinates are calculated at the first run and the picture is positioned correctly at subsequent runs.

If, for some reason, this doesn't work, then you can add \usepackage{everypage} in the preamble and put the tikzpicture below the table using \AddThispageHook{...} like this:

\AddThispageHook{%
\begin{tikzpicture}[overlay,remember picture]
\node [yshift=.6ex] at ( $(pic cs:A) !.5! (pic cs:B)$ ){ \fontsize{60}{60}\selectfont\textbf{\color{gray!20}A} };
\end{tikzpicture}
}

-

\documentclass{article}
\usepackage{color}
\usepackage{lmodern}
\begin{document}

\begin{picture}(0,0)
\put(7,-20){\fontsize{70pt}{70pt}\bfseries\color[gray]{.9}{A}}
\end{picture}%
\begin{tabular}{llll}
1 & 2 & 3 & 4 \\
1 & 2 & 3 & 4 \\
1 & 2 & 3 & 4 \\
1 & 2 & 3 & 4 \\
1 & 2 & 3 & 4
\end{tabular}

\end{document}


By reducing the font size and altering the coordinates you can of course just place the letter (or letters) behind any sub-matrix block.

-
thanks! that's what I meant. is there a way to specify the coordinate not in an absolute manner, but refer to an anchor in one of the table cells? (so that if the table's position changes, i don't have to recalculate the coordinates) – tabular Jan 17 at 21:46
@tabular yes but not this evening:-) one of the tikz gang will probably give a tikz answer if you wait a bit, it's easier if you load a million lines of tikz package setup first:-) You can put the picture at the start of the top left cell of your block rather than before the table, that anchors it at your block, but automatically picking up the table cell widths is a bit trickier – David Carlisle Jan 17 at 22:02

Here's a TikZ solution which uses a matrix of nodes for the table, rather than a tabular, and hence gets the positions for the letters automatically from the TikZ code. It uses the matrix library for that and the backgrounds library to put the letters on a layer drawn in the background i.e. behind the numbers in the matrix.

\documentclass[border=5pt, multi, tikz]{standalone}
\usepackage[rm={lining},sf={lining},tt={lining,tabular,monowidth}]{cfr-lm}
\usetikzlibrary{backgrounds,matrix}
\begin{document}
\begin{tikzpicture}
[
font=\tstyle,
big alph/.style={text opacity=.2, text=gray, font=\Huge}
]
\matrix (m) [matrix of nodes]
{
1 & 2 & 3 & 4 \\
1 & 2 & 3 & 4 \\
1 & 2 & 3 & 4 \\
1 & 2 & 3 & 4 \\
};
\begin{scope}[on background layer, big alph]
\node at (m-1-1.south east) {A};
\node at (m-1-3.south east) {B};
\node at (m-3-1.south east) {C};
\node at (m-3-3.south east) {D};
\end{scope}
\end{tikzpicture}
\end{document}


If your table includes more complex mathematics, use matrix of math nodes to have the nodes automatically switch to maths mode.

The (m) gives the matrix its name which is then used to position the letters by referencing the matrix's name, the row and the column (e.g. m-1-1 etc.). The anchor of the node south east is the relevant position on the border of the individual node within the matrix i.e. bottom left for south east. So (m-3-3.north west) would refer to the top right corner of the node in the third column and third row of the matrix named m.

-

I introduce \grayblock{tabular code}{x-fractional offset}{y-fractional offset}{scale of inset}{inset}. The macro can be nested (as it is in the MWE below) to place multiple insets, where a \grayblock can be placed where tabular code would go.

\documentclass{article}
\usepackage{stackengine,xcolor,graphicx}
\newsavebox{\tmpbox}
\newcommand\grayblock[5]{%
\savebox{\tmpbox}{#1}%
\stackinset{c}{}{c}{}{\usebox{\tmpbox}}%
{\stackinset{l}{#2\wd\tmpbox}{b}{#3\dimexpr\ht\tmpbox+\dp\tmpbox}{%
\color{black!20}\scalebox{#4}{#5}}{%
\rule{\wd\tmpbox}{0pt}\rule[-\dp\tmpbox]{0pt}{\dimexpr\ht\tmpbox+\dp\tmpbox}}}%
}
\begin{document}
\fboxsep=0pt
x
\grayblock{%
\grayblock{\begin{tabular}{|cccc|}
\hline
a & b & c & d\\
d & e & f & g\\
g & h & i & j\\
j & k & l & m\\\hline
\end{tabular}}{.1}{.53}{3}{B}%
}{.57}{.08}{3}{C}
x
\end{document}


-

Lazy way: tcolorbox

\documentclass{article}
\usepackage{tcolorbox}
\tcbuselibrary{skins}
\newtcbox{\mytable}[1][X]{enhanced, blanker, on line, watermark text=#1}
\begin{document}
My tables
\mytable[A]{
\begin{tabular}{llll}
1 & 2 & 3 & 4 \\
1 & 2 & 3 & 4 \\
1 & 2 & 3 & 4 \\
1 & 2 & 3 & 4 \\
1 & 2 & 3 & 4
\end{tabular}}
with watermarks
\mytable{\begin{tabular}{lll}  1 & 2 \\3 & 4 \end{tabular}}
\end{document}

-