How to combine \ifdefstring (etoolbox) with \multicolumn
The problem is that \ifdefstring
is not expandable, therefore it cannot be used at this place of a table. You can overcome this by using the low-level \ifx
:
\documentclass[10pt,a4paper,twoside]{article}
\usepackage{etoolbox}
\usepackage{booktabs}
\newcommand{\ReportLanguage}{no}
% two alternative values for \ReportLanguage:
% "no" for norwegian and "en" for english
\newcommand{\NorwegianLanguage}{no}
\begin{document}
\begin{tabular}{ccc}
\ifx\ReportLanguage\NorwegianLanguage
\multicolumn{2}{c}{Utslippspunkt} & Salinitet \\
Dybde & temperatur & \\
\else
\multicolumn{2}{c}{Release point} & Salinity \\
Depth & temperature & \\
\fi
\midrule
100 & 10 & 32 \\
150 & 7 & 33 \\
180 & 4 & 35 \\
\bottomrule
\end{tabular}
\end{document}
At a beginning of a table cell/line, LaTeX looks for things like \multicolumn
, \hline
etc. When you put something non-expandable there, LaTeX simply stops looking for these things. And if they are found later (like your \multicolumn
), LaTeX is confused and throws an error.
Just a remark: I think you want twoside
instead of twosided
(maybe both work, I dunno, I always use the first one).
If you don't want to define \NorwegianLanguage
, you can do this (USE AT YOUR OWN RISK):
\documentclass[10pt,a4paper,twosided]{article}
\usepackage{etoolbox}
\usepackage{booktabs}
\newcommand{\ReportLanguage}{no}
% two alternative values for \ReportLanguage:
% "no" for norwegian and "en" for english
\begin{document}
\begin{tabular}{ccc}
\expandafter\ifx\csname\ReportLanguage LANGUAGE\endcsname\noLANGUAGE
\multicolumn{2}{c}{Utslippspunkt} & Salinitet \\
Dybde & temperatur & \\
\else
\multicolumn{2}{c}{Release point} & Salinity \\
Depth & temperature & \\
\fi
\midrule
100 & 10 & 32 \\
150 & 7 & 33 \\
180 & 4 & 35 \\
\bottomrule
\end{tabular}
\end{document}
I'd use a more user-friendly approach: you issue a \TableHeader
command that contains a key-value list of the headers you want for the next table, then in the table you place \UseTableHeader
.
In the example I switch language (which probably you don't want to do in a real document) just to show the result; notice that the values given as argument to \TableHeader
survive until another \TableHeader
is issued again.
\documentclass[10pt,a4paper,twoside]{article}
\usepackage{booktabs}
\usepackage{keyval}
\makeatletter
\define@key{espentable}{no}{\def\espen@header@no{#1}}
\define@key{espentable}{en}{\def\espen@header@en{#1}}
\newcommand{\TableHeader}[1]{%
\setkeys{espentable}{#1}%
\ignorespaces
}
\newcommand{\UseTableHeader}{\@nameuse{espen@header@\espen@language}}
% two alternative values for \ReportLanguage:
% "no" for norwegian and "en" for english
\newcommand{\ReportLanguage}[1]{\def\espen@language{#1}}
\def\espen@language{en}% default
\makeatother
\ReportLanguage{no}
\begin{document}
Norsk:
\TableHeader{
no={\multicolumn{2}{c}{Utslippspunkt} & Salinitet \\
Dybde & temperatur & \\},
en={\multicolumn{2}{c}{Release point} & Salinity \\
Depth & temperature & \\}
}
\begin{tabular}{ccc}
\toprule
\UseTableHeader
\midrule
100 & 10 & 32 \\
150 & 7 & 33 \\
180 & 4 & 35 \\
\bottomrule
\end{tabular}
\bigskip
English:
\ReportLanguage{en}
\begin{tabular}{ccc}
\toprule
\UseTableHeader
\midrule
100 & 10 & 32 \\
150 & 7 & 33 \\
180 & 4 & 35 \\
\bottomrule
\end{tabular}
\end{document}
This has the advantage that it's easily extendable to more than two languages.