Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

This example works but if I try to put an optional argument changing

\newcommand\TBLdiag[5]{%

in

\newcommand\TBLdiag[5][\tabcolsep]{%

it does not work anymore. Of course I call \TBLdiag with only four arguments then.

\documentclass[10pt,french]{article}
\usepackage[T1]{fontenc}
\usepackage{babel}
\usepackage[locale=FR]{siunitx}
\usepackage{array}
\usepackage{calc}
\usepackage{tikz,fourier,ragged2e}

\newcommand\TBLdiag[5]{%
    \multicolumn{1}{|m{#2}|}{%
    \hskip-\tabcolsep
    \begin{tikzpicture}[baseline=0,anchor=south west,outer sep=0]
        \path[use as bounding box] (0,0)
            rectangle (#2+2\tabcolsep,\baselineskip);
        \node[minimum width={#2+2\tabcolsep-\pgflinewidth},
            minimum  height=\baselineskip+#3-\pgflinewidth]
            (box) {};
        \draw[line cap=round]
            (box.north west) -- (box.south east);
        \node[anchor=south west,align=left,inner sep=#1]
            at (box.south west) {#4};
        \node[anchor=north east,align=right,inner sep=#1]
            at (box.north east) {#5};
        \end{tikzpicture}%
    \rule{0pt}{\baselineskip+#3-\pgflinewidth}%
    \vspace{-.3\baselineskip}%\hskip-\tabcolsep
    }
}

\begin{document}
\begin{tabular}{%
    |>{\Centering}m{4.5cm}
    |*{4}{>{\Centering}m{2.5em}|}}\hline

\TBLdiag{\tabcolsep}{4.5cm}{3.4em}{Coefficient\\ de frottement $c$}{Vitesse $V$\\ (en km/h)}&50&90&110&130\\ \hline
$c=\num{0,8}$\newline route s\`eche, bitume neuf &&&&\\ \hline
$c=\num{0,7}$\newline route s\`eche, bitume moyen &&&&\\ \hline
$c=\num{0,3}$\newline route mouill\'ee, bitume moyen &&&&\\ \hline
\end{tabular}

\end{document}

Thanks to Steven B. Segletes there is a minimal example that works / doesn't works :

\documentclass{article}
\usepackage{array}

\newcommand\TBLdiag[2]{% WORKS
%\newcommand\TBLdiag[2][]{% DOESN'T WORK     
\multicolumn{1}{|m{3.4em}|}{X} }

\begin{document}
\begin{tabular}{|l|c|c|c|c|}\hline
\TBLdiag{0pt}{4.5cm}&50&90&110&130\\ \hline% WORKS
%\TBLdiag[0pt]{4.5cm}&50&90&110&130\\ \hline% DOESN'T WORK
\end{tabular}
\end{document}
share|improve this question
    
Do you always want to have inner sep = \tabcolsep? Why not put it in your command definition then? Replace all #1 by \tabcolsep, change your command to \newcommand\TBLdiag[4]{% and adapt all #-numbers in the command in order to count from 1 to 4. Did I get you right? –  LaRiFaRi May 6 at 8:39
1  
Not always, but often ;-) As a close friend always says : "tell me what you want, I can explain you how to live without" ;-) –  Tarass May 6 at 8:44
1  
Thank you for your concern. I'd like to know why this optional argument makes troubles. Is this a mystake of mine, or there is a trick that one has to know that in certain circumstances using an optional argument must be done with carefullness (maybe because it is an length ?). It can append in other circumstances ... –  Tarass May 6 at 9:03
2  
I've narrowed it a bit more... it concerns the use of an optional argument with the use of \multicolumn in the called routine. –  Steven B. Segletes May 6 at 11:03
1  
Simpler version that works/doesn't work. You may want to add this as an addendum to your question. \documentclass{article} \usepackage{array} \usepackage{tikz,fourier,ragged2e} \newcommand\TBLdiag[2]{% WORKS %\newcommand\TBLdiag[2][]{% DOESN'T WORK \multicolumn{1}{|m{3.4em}|}{X} } \begin{document} \begin{tabular}{% |l|c|c|c|c|}\hline \TBLdiag{0pt}{4.5cm}&50&90&110&130\\ \hline% WORKS %\TBLdiag[0pt]{4.5cm}&50&90&110&130\\ \hline% DOESN'T WORK \end{tabular} \end{document} –  Steven B. Segletes May 6 at 11:24

1 Answer 1

up vote 6 down vote accepted

\multicolumn can never be hidden in a command defined with an optional argument with \newcommand. This has been discussed before: TeX needs to expand macros in order to see whether \multicolumn is present, but with such a command it can't find it in time.

You can do with xparse; I know that the second example is wrong and is just to see that the optional argument works. But of course this is just for showing how you could do; please, avoid tables constructed in that way: the diagonal box is simply horrible.

\documentclass[10pt,french]{article}
\usepackage[T1]{fontenc}
\usepackage{babel}
\usepackage[locale=FR]{siunitx}
\usepackage{array}
\usepackage{calc}
\usepackage{tikz,fourier,ragged2e,xparse}

\DeclareExpandableDocumentCommand\TBLdiag{O{\tabcolsep}mmmm}{%
    \multicolumn{1}{|m{#2}|}{%
    \hskip-#1
    \begin{tikzpicture}[baseline=0,anchor=south west,outer sep=0]
        \path[use as bounding box] (0,0)
            rectangle (#2+2\tabcolsep,\baselineskip);
        \node[minimum width={#2+2\tabcolsep-\pgflinewidth},
            minimum  height=\baselineskip+#3-\pgflinewidth]
            (box) {};
        \draw[line cap=round]
            (box.north west) -- (box.south east);
        \node[anchor=south west,align=left,inner sep=#1]
            at (box.south west) {#4};
        \node[anchor=north east,align=right,inner sep=#1]
            at (box.north east) {#5};
        \end{tikzpicture}%
    \rule{0pt}{\baselineskip+#3-\pgflinewidth}%
    \vspace{-.3\baselineskip}%\hskip-\tabcolsep
    }
}

\begin{document}
\begin{tabular}{%
    |>{\Centering}m{4.5cm}
    |*{4}{>{\Centering}m{2.5em}|}}\hline
\TBLdiag{4.5cm}{3.4em}{Coefficient\\ de frottement $c$}{Vitesse $V$\\ (en km/h)}&50&90&110&130\\ \hline
$c=\num{0,8}$\newline route s\`eche, bitume neuf &&&&\\ \hline
$c=\num{0,7}$\newline route s\`eche, bitume moyen &&&&\\ \hline
$c=\num{0,3}$\newline route mouill\'ee, bitume moyen &&&&\\ \hline
\end{tabular}

\begin{tabular}{%
    |>{\Centering}m{4.5cm}
    |*{4}{>{\Centering}m{2.5em}|}}\hline
\TBLdiag[2\tabcolsep]{4.5cm}{3.4em}{Coefficient\\ de frottement $c$}{Vitesse $V$\\ (en km/h)}&50&90&110&130\\ \hline
$c=\num{0,8}$\newline route s\`eche, bitume neuf &&&&\\ \hline
$c=\num{0,7}$\newline route s\`eche, bitume moyen &&&&\\ \hline
$c=\num{0,3}$\newline route mouill\'ee, bitume moyen &&&&\\ \hline
\end{tabular}

\end{document}

enter image description here

Here's how I would typeset the table:

\documentclass[10pt,french]{article}
\usepackage[T1]{fontenc}
\usepackage{babel}
\usepackage[locale=FR]{siunitx}
\usepackage{array}
\usepackage{fourier,booktabs}

\newcommand{\smashedcell}[1]{%
  \smash{\begin{tabular}[b]{@{}c@{}}\strut#1\strut\end{tabular}}%
}

\begin{document}
\begin{tabular}{%
    >{\centering}m{4.5cm}
    *{4}{>{\centering\arraybackslash}m{2.5em}}}
\toprule
& \multicolumn{4}{c}{Vitesse $V$ (en \si{km/h})} \\
\cmidrule{2-5}
\multicolumn{1}{c}{\smashedcell{Coefficient\\ de frottement $c$}} &50&90&110&130\\ 
\midrule
$c=\num{0,8}$\\ route s\`eche, bitume neuf &&&&\\
\midrule
$c=\num{0,7}$\\ route s\`eche, bitume moyen &&&&\\
\midrule
$c=\num{0,3}$\\ route mouill\'ee, bitume moyen &&&&\\
\bottomrule
\end{tabular}

\end{document}

enter image description here

share|improve this answer
    
Thank for the explanations about multicolumn. Concerning the new typeseting of the table, I agree with you that it is very redeable, as it is for a scholar use, I need something usual for the children. I saw the very good animation on how to make ckear tables, and I agree with it. –  Tarass May 6 at 12:18
    
btw You add an unnecessary \hskip-#1 as #1 is only for inner sep the rest is carrectly balanced. Concerning diagonal boxes, as I said, in France (and maybe other places), are ofen used and people should be able to understand them and/or to work with them. Maybe you may propose a scholar reform ;-) –  Tarass May 6 at 12:27
    
@Tarass Oh, sorry! I couldn't see #1! –  egreg May 6 at 12:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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