如何用 tikz 節點填充表格框並將文字水平置中?

如何用 tikz 節點填充表格框並將文字水平置中?

如何使用 tikz 節點填充表格框並將文字水平置中?

在這種情況下我不想使用 colortbl 套件。

在此輸入影像描述

\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}

答案1

我不確定我是否理解你的問題,但如果你想將 a 與 b 和 c 垂直對齊,請考慮

\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}

在此輸入影像描述

更新:好的,我看到@Zarko 已經給了你想要的東西。為了好玩,我遵循了你原來的方法,這就是我得到的:

\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}

在此輸入影像描述

它確實正確地計算了行的高度(我認為),但只有在我使用了一行有兩行的事實之後。而且垂直對齊並不完美。我只是將其發布,以便以後需要時可以完成。

答案2

我懷疑你正在尋找這個:

在此輸入影像描述

為此,你不需要魔法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}

附錄(已編輯): 來自下面評論中的討論*以及您的後續行動 問題* 我的結論是,你實際上喜歡擁有解決方案類似於我在該(後續)問題中提供的答案中的第一個範例:

在此輸入影像描述

*這使得您可以進一步更改節點形狀,正如您在下面的評論中提到的那樣,並在後續問題中進一步詳細說明:

\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}

相關內容