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}

관련 정보