![可选参数问题](https://linux22.com/image/289877/%E5%8F%AF%E9%80%89%E5%8F%82%E6%95%B0%E9%97%AE%E9%A2%98.png)
这个例子有效,但如果我尝试把一个可选参数改变
\newcommand\TBLdiag[5]{%
在
\newcommand\TBLdiag[5][\tabcolsep]{%
它不再起作用了。当然,我\TBLdiag
当时只用四个参数来调用。
\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}
感谢Steven B. Segletes
有一个有效/无效的最小示例:
\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}
答案1
\multicolumn
永远不能隐藏在用 定义的可选参数的命令中\newcommand
。这之前已经讨论过了:TeX 需要扩展宏才能查看是否\multicolumn
存在,但使用这样的命令它无法及时找到它。
你可以这样做xparse
;我知道第二个例子是错误的,只是为了看看可选参数是否有效。但这当然只是为了展示你可以怎么做;请避免以这种方式构造表格:对角线框简直太糟糕了。
\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}
以下是我排版表格的方法:
\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}