2

How to fill a tabular box with a tikz node and center h/v the text ?

I don't want to use colortbl package in this case.

enter image description here

\documentclass[a4paper]{article}

\usepackage{array,tabularx,tikz,ragged2e,siunitx}
\begin{document}

{\renewcommand\tabularxcolumn[1]{m{#1}}
\renewcommand{\arraystretch}{1.3}
\begin{tabularx}{\linewidth}{%
    |X|*{3}{>{\Centering}m{2cm}|}}\hline
Entourer la ou les bonne(s) reponse(s)
&\tikz[baseline,overlay]
    \node[fill=blue!15,minimum height=20pt,minimum width=2cm+2\tabcolsep] (A) {a};
&b&p\\\hline
\end{tabularx}}

\end{document}
Tarass
  • 16,912

2 Answers2

3

i suspect that you looking for this:

enter image description here

for this you not need magic with tikz:

\documentclass[a4paper]{article}
\usepackage{array,tabularx}%reorganized loading of package
\renewcommand\tabularxcolumn[1]{m{#1}} % added
\usepackage{ragged2e}
%\usepackage{tikz}      % is not used in mwe
%\usepackage{siunitx}   % is not used in mwe
%
\usepackage[table]{xcolor}% new package, for coloring table

\begin{document}
\renewcommand{\arraystretch}{1.3}
\begin{tabularx}{\linewidth}{|X|*{3}{>{\Centering}m{2cm}|}}
    \hline
Entourer la ou les bonne(s) reponse(s)
    & \cellcolor{blue!15}{a} & b & p    \\
    \hline
\end{tabularx}
\end{document}

addendum (edited): from discussion in comments below *and from your follow-up question * i conclude, that you actually like to have solution similar to first example in my answer as i provide on that (follows-up) question :

enter image description here

*which enable your further changes of node shape as you mentioned in comment below and further elaborate in your follow-up question:

\documentclass[12pt]{article}
\usepackage{tabularx}
\renewcommand\tabularxcolumn[1]{m{#1}}
\usepackage{ragged2e}
\usepackage{tikz}

\newcommand{\DC}[1]{%
\begin{tikzpicture}[baseline=(current bounding box.base)]
    \node[minimum width=\dimexpr2cm+2\tabcolsep,
          minimum height=12mm, text depth=0.25ex,
          inner ysep=2mm, outer sep=0pt,
          append after command={
             \pgfextra{\let\LN\tikzlastnode
          \path[fill=blue!15]
            (\LN.south west) -| (\LN.north east) -| cycle;
                      }     },
          font=\bfseries] {#1};
\end{tikzpicture}}

\begin{document}
    \begingroup
\renewcommand{\arraystretch}{1.3}
\begin{tabularx}{\linewidth}{%
    |>{\raggedright}X | *{3}{@{}>{\Centering}m{\dimexpr2cm+2\tabcolsep}@{}|}
                            }
    \hline
Entourer la ou les bonne(s) reponse(s)
    & \DC{a}   &    b   &   c    \\
    \hline
\end{tabularx}
    \endgroup
\end{document}
Zarko
  • 296,517
  • Thank you. But I want to use a node to round its corners after. – Tarass Feb 13 '18 at 17:02
  • @Tarass, well, than my answer is not for you (you question is not clear at all, sorry) . before i will delete it, please see https://wiert.me/2014/04/03/andre-vatter-google-wie-tabellen-eigentlich-aussehen-sollten-%EF%BB%BF/ – Zarko Feb 13 '18 at 17:06
  • I thank you for your concern. fill a tabular box with a tikz node. Tell me I to be more specific : fill -- full place is taken, tabular box -- it's clear, and tikz node also. I know that in anglo-saxon tradition there is not much line, and I appriceate that, but in France and in school, people are used to square boxes ;-) – Tarass Feb 13 '18 at 17:12
  • It's for fun, I don't really need to have this top rounded box, but as they exist in one book I am reading and one see them ofen, I ask my self, could it be done in Latex. I made a mwe and I ask my self where is stored the height of a tabular row ? – Tarass Feb 13 '18 at 17:28
  • Btw, filling a tabular box with a node could be usefull in another case. – Tarass Feb 13 '18 at 17:29
  • I accpeted mamot's answer. It is closer to tabular and tikz together. Thank's anyway. – Tarass Feb 13 '18 at 19:27
  • Do not ! I agree with the simple solution, and I maybe (certainly) never will use this. But I still try to make it : https://tex.stackexchange.com/questions/415187/how-to-guess-and-use-the-height-of-a-tabular-row – Tarass Feb 13 '18 at 20:16
  • @Tarass, i edit my answer. now i consider your follow-up question https://tex.stackexchange.com/questions/415187/how-to-guess-and-use-the-height-of-a-tabular-row. now all is better clear to me :-) – Zarko Feb 14 '18 at 06:27
1

I am not sure if I understand your question, but if you wish to align a vertically with b and c, consider

\documentclass[a4paper]{article}

\usepackage{array,tabularx,tikz,ragged2e,siunitx}
\begin{document}

{\renewcommand\tabularxcolumn[1]{m{#1}}
\renewcommand{\arraystretch}{1.3}
\begin{tabularx}{\linewidth}{%
    |X|*{3}{>{\Centering}m{2cm}|}}\hline
Entourer la ou les bonne(s) reponse(s)
&\tikz[baseline=(A.base),overlay]
    \node[fill=blue!15,minimum height=20pt,minimum width=2cm+2\tabcolsep] (A) {a};
&b&p\\\hline
\end{tabularx}}

\end{document}

enter image description here

UPDATE: OK, I see that @Zarko already gave you what you wanted. For fun, I followed a bit your original approach, and this is where I got:

\documentclass[a4paper]{article}

\usepackage{array,tabularx,tikz,ragged2e,siunitx}
\usetikzlibrary{calc}
\newcommand{\TikzStrut}[1]{\tikz[overlay,remember picture]{\node(#1){\strut};}}
\makeatletter
\newcommand{\RowHeight}{% see e.g. https://tex.stackexchange.com/a/84536/121799
\def\tmp{\dimexpr\arraystretch\ht\strutbox+\arraystretch\ht\strutbox+\arraystretch\dp\@arstrutbox}\relax
\xdef\myrowheight{\the\tmp}\relax
}
\makeatother
\begin{document}

{\renewcommand\tabularxcolumn[1]{m{#1}}
\renewcommand{\arraystretch}{1.3}
\begin{tabularx}{\linewidth}{%
    |X|*{3}{>{\Centering}m{2cm}|}}\hline
Entourer la ou les bonne(s) reponse(s)\RowHeight
&\tikz[baseline=(A.base),overlay,remember picture]{
    \node[fill=blue!15,minimum height=\myrowheight,minimum width=2cm+2\tabcolsep] (A) 
    {a};}
&b&p\\\hline
\end{tabularx}}

\end{document}

enter image description here

It does compute the height of the row correctly (I think) but only after I used the fact that there is a row with two lines. And also the vertical alignment is not perfect. I'm just posting that so that it can be completed if needed later.

  • It is good start, but real problem is to fill vertically the box with the node, ie I want it full blue. I don't know how fetch the row height. – Tarass Feb 13 '18 at 17:00
  • I prefer your approach. What is remember picture for in this case ? Don't see the difference with or without. The node still a bit vertically low. – Tarass Feb 13 '18 at 19:25
  • @Tarass The remember picture is an artifact of an attempt that ended in Nirvana. –  Feb 13 '18 at 19:40
  • @marmot, you also should show, that node in cell has rounded corners (as op told me in comment to my solution)... – Zarko Feb 13 '18 at 19:44
  • @Zarko Luckily, you already explained that to him. ;-) I stressed that I posted this just for fun and that there are severe problems with the current version of my code (as opposed to yours). I'd only care about such details if I were to master the bigger challenges, which I do not (yet). –  Feb 13 '18 at 19:48