5

I try to adapt this solution How to diagonally divide a table cell … properly? to my needs.

But I have a width problem 1\tabcolsep is too short, but anything longer is too long, should be 2\tabcolsep.

enter image description here

\documentclass[10pt]{article}
\usepackage[T1]{fontenc}

\usepackage{array}
\usepackage{tabularx}
\usepackage{ragged2e}

\newcolumntype{C}[1]{>{\Centering}p{#1}}
\usepackage{tikz}

\newcommand\Tdiag[4]{%
    \multicolumn{1}{|p{#2}|}{\hskip-\tabcolsep
    \begin{tikzpicture}[%
                baseline={(0,-.25\baselineskip)},
                every node/.style={outer sep=0pt,inner sep=#1}]
    \node[minimum width={#2+1\tabcolsep-\pgflinewidth},
        minimum height=2\baselineskip-\pgflinewidth+\extrarowheight,
        use as bounding box] (box) {};
    \draw[line cap=round] (box.north west) -- (box.south east);
    \node[anchor=south west,text width=.75*#2,align=left] at (box.south west) {#3};
    \node[anchor=north east,text width=.75*#2,align=right] at (box.north east) {#4};
\end{tikzpicture}\hskip-\tabcolsep}}

\begin{document}

\begin{tabular}{|C{1.2cm}*{3}{|c}|}\hline
\Tdiag{.2em}{1.2cm}{\^Age}{Sexe}&
Féminin & Masculin & Totaux  \\\hline
15 ans&&1&\\\hline
16 ans & 16 & &  \\\hline
17 ans & 3 & &  \\\hline
Totaux & & & 36  \\\hline
\end{tabular}


\end{document}
Tarass
  • 16,912

1 Answers1

8

You don't need TikZ for that. A simple diagbox package will do the trick:

\documentclass[11pt, a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern} 
\usepackage{diagbox}

\begin{document}

\begin{table}\centering\setlength\tabcolsep{3.5pt}\renewcommand\arraystretch{1.25}
  \noindent\makebox[\textwidth]{%
    \begin{tabular}{|l|*{3}{c|}}
      \hline
      \diagbox[width=\dimexpr \textwidth/8+2\tabcolsep\relax, height=1cm]{ Âge }{Sexe}
                   & Féminin & Masculin & Totaux \\
      \hline
      15 ans & & 1 & \\
      \hline
      16 ans & 16 & & \\
      \hline17 ans & 3 & & \\
      \hline
      Totaux & & & 36 \\
      \hline
    \end{tabular}
  }%
\end{table}

\end{document} 

enter image description here

Bernard
  • 271,350