3

I would like to create a Latex table with a line on the main diagonal. My problem: diagbox does not calculate the height in the right way for multirow fields. A small example:

\documentclass[a4paper]{article}
\usepackage{calc}
\usepackage{diagbox}

\begin{document}
\begin{tabular}{p{\textwidth*1/7}*{3}{|p{\textwidth*1/7}}}
\diagbox[innerwidth=\textwidth*1/7]{from}{to} & item1 & item2 & item3 \\\hline
item1 & \diagbox[innerwidth=\textwidth*1/7]{~}{~} & --- this --- is --- a --- very --- long --- multirow --- text --- & \\\hline
item2 & & \diagbox[innerwidth=\textwidth*1/7]{~}{~} &   \\\hline
item3 & & & \diagbox[innerwidth=\textwidth*1/7]{~}{~}
\end{tabular}
\end{document}

diagbox and multirow

How can I connect the diagonal lines?

Greeting, Patrick

2
  • Is the diagonal to be straight all over the table or only on the single cells? – CarLaTeX Nov 4 '17 at 14:23
  • The diagonal should be straigt only on the single cells. – PSaR Nov 4 '17 at 15:34
1

I don't know why if you use height=... option of \diagbox the text of the following multiline cell is shifted down, but you can work around it adding a \vspace{-...} to bring it up again.

However, I suggest to you to redesign your table with no vertical rules, no diagonal boxes, and em-dashes as text of the cells on the diagonal.

\documentclass[a4paper]{article}
\usepackage{booktabs}
\usepackage{diagbox}

\begin{document}
\noindent With a little workaround:
\begin{center}
    \begin{tabular}{p{\textwidth*1/7}*{3}{|p{\textwidth*1/7}}}
        \diagbox[innerwidth=\textwidth*1/7]{from}{to} & item1 & item2 & item3 \\\hline
        item1 & \diagbox[innerwidth=\textwidth*1/7,height=7\line]{~}{~} & \vspace{-9ex}--- this --- is --- a --- very --- long --- multirow --- text --- & \\\hline
        item2 & & \diagbox[innerwidth=\textwidth*1/7]{~}{~} &   \\\hline
        item3 & & & \diagbox[innerwidth=\textwidth*1/7]{~}{~}
    \end{tabular}   
\end{center}
\noindent But I suggest to you to use something like this:
\begin{center}
    \begin{tabular}{*{4}{p{\textwidth*1/7}}}
        \toprule
        & \multicolumn{3}{c}{To:}\\
        \cmidrule{2-4}
        From: & item1 & item2 & item3 \\
        \midrule
        item1 & \textemdash & --- this --- is --- a --- very --- long --- multirow --- text --- & \\
        item2 & & \textemdash & \\
        item3 & & & \textemdash \\
        \bottomrule
    \end{tabular}
\end{center}
\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.