7

I need to make tabulars with either norwegian or english column heading above the numbers. I have defined a command, \ReportLanguage, that can be set to either of two values, "no" for norwegian, and "en" for english. This language choise command is then combined with the etoolbox function \ifdefstring to make a switch between norwegian and english header text above the tabular content. This language switch function is shown below:

\documentclass[10pt,a4paper,twosided]{article}
\usepackage{etoolbox}
\usepackage{booktabs}

\newcommand{\ReportLanguage}{en}
% two alternative values for \ReportLanguage: 
% "no" for norwegian and "en" for english

    \begin{document} 
    \begin{tabular}{ccc}
          \ifdefstring{\ReportLanguage}{no}%
              {Dybde & temperatur & Salinitet  \\}
              {Depth & temperature  & Salinity  \\}                           
                \midrule
                100 & 10 & 32 \\
                150 & 7  & 33 \\            
                180 & 4  & 35 \\
                \bottomrule
    \end{tabular}
    \end{document}

This works fine for either two values of the \ReportLanguage variable, and gives the following compiled result when \ReportLanguage is set to the value "en".

enter image description here

However, I would like to have a common heading above the two first headings, the "Depth" and "temperature" headings. I have tried to use the \multicolumn function inside the \ifdefstring language switch function, but I get an error message. The code looks as this:

\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}
      \ifdefstring{\ReportLanguage}{no}%
          {\multicolumn{2}{c}{Utslippspunkt} & Salinitet  \\       
             Dybde & temperatur                &             \\}
          {\multicolumn{2}{c}{Release point}  & Salinity  \\                           
            Depth & temperature                &            \\}
            \midrule
            100 & 10 & 32 \\
            150 & 7  & 33 \\            
            180 & 4  & 35 \\
            \bottomrule
\end{tabular}
\end{document}

and the error message looks as this:

enter image description here

What is wrong with my code? I will appreciate help on this topic, thanks in advance!

  • +1 for recognizing where exactly the error lies and for a very good minimal example! – yo' Nov 14 '13 at 12:44
6

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}
  • thanks a lot! But, could your solution work without the new command \NorwegianLanguage? could "\ifx\ReportLanguage\NorwegianLanguage" be exchanged with "\ifx\ReportLanguage no" ? – myotis Nov 14 '13 at 12:46
  • It can, but it's a very dirty trick, I'll update in a minute – yo' Nov 14 '13 at 12:51
  • That is a very dirty trick. One might expect the \ifx test to be always true (because the compared macros \enLANGUAGE and \noLANGUAGE are both undefined) or always false (because the \csname...\endcsname of an undefined macro is \relax). But TeX's treatment of \expandafter\ifx\csname... is very strange (to me). Also odd is \expandafter\ifx\noexpand\cmd\cmd, which is true for unexpandable commands and false for expandable ones! – Dan Nov 14 '13 at 22:07
2

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.

enter image description here

  • Thanks a lot ! I will dive into your solution and study it in detail : ) – myotis Nov 14 '13 at 13:45

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.