1

I have a tabularx with 4 columns and 2 2-3 rows. The problem is that even though the first row behaves as expected, the second one doesn't. I am trying to set the 3rd column right aligned and the 4th column center. However, I cannot get it to work on all rows. I have created a minimum sample, that shows both my setup and my problem.

In the sample code, I am creating two tables, just to show the differences.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{hyperref}
\usepackage{tabularx}           % Til tabeller der tilpaser sig i bredden
\newcolumntype{R}{>{\raggedleft\arraybackslash}r}

\usepackage{xifthen}            % Used to test if an argument is empty
\usepackage{datetime}           % Used to format dates
\newdateformat{timeLineDate}{\THEDAY.~\shortmonthname~\THEYEAR}
\newdateformat{monthYearDate}{\monthname[\THEMONTH]~\THEYEAR}

\newcommand{\createPerson}[9]
{
        \begin{tabularx}{\textwidth}{lXRc}
            Navn:       &   #1                                     &   \formatdate{#4}{#5}{#6}  &   $\star$\\
            \ifNoArgumentElse{#2}{Kaldenavn: & #3}{Fødenavn:&#2}&   \ifNoArgumentElse{#7#8#9}{&}{
                \ifNoArgumentElse{#7}{
                    \ifNoArgumentElse{#8}{#9}{\monthYearDate\formatdate{1}{#8}{#9}}
                }{\formatdate{#7}{#8}{#9}} & \cross
            }
            \ifNoArgumentElse{#2}{}{\\Kaldenavn: & #3 &&}
        \end{tabularx}
}

%Arguments
%   1) The argument to test
%   2) If empty then do this
%   3) If not empty then do this
\newcommand{\ifNoArgumentElse}[3]{
  \ifthenelse{\isempty{#1}}{#2}{#3}%
}

\newcommand{\cross}[1][1pt]{\ooalign{%
  \rule[1ex]{1ex}{#1}\cr% Horizontal bar
  \hss\rule{#1}{.7em}\hss\cr}% Vertical bar
}

\begin{document}

\subsection{Else Jensen}
\label{person1}

\createPerson
    {\nameref{person1}}
    {Else Nielsen}
    {Else}
    {11}
    {9}
    {1940}
    {13}
    {11}
    {1999}


\subsection{Hans Wright}
\label{person2}

\createPerson
    {\nameref{person2}}
    {}
    {Hans}
    {11}
    {9}
    {1940}
    {}
    {11}
    {1999}

\end{document}
7heViking
  • 1,183

1 Answers1

1

If I did understand you right, your problem is an easy one, you have to use % at the end of lines and be careful that there are no unnecessary blank spaces anywhere.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{hyperref}
\usepackage{tabularx}           % Til tabeller der tilpaser sig i bredden
\newcolumntype{R}{>{\raggedleft\arraybackslash}r}

\usepackage{xifthen}            % Used to test if an argument is empty
\usepackage{datetime}           % Used to format dates
\newdateformat{timeLineDate}{\THEDAY.~\shortmonthname~\THEYEAR}
\newdateformat{monthYearDate}{\monthname[\THEMONTH]~\THEYEAR}

\newcommand{\createPerson}[9]
{
        \begin{tabularx}{\textwidth}{l|X|R|c}
            Navn: & #1 & \formatdate{#4}{#5}{#6} & $\star$\\
            \ifNoArgumentElse{#2}{Kaldenavn: & #3}{Fødenavn:&#2} & \ifNoArgumentElse{#7#8#9}{&}{%
                \ifNoArgumentElse{#7}{%
                    \ifNoArgumentElse{#8}{#9}{\monthYearDate\formatdate{1}{#8}{#9}}%
                }{\formatdate{#7}{#8}{#9}} & \cross%
            }%
            \ifNoArgumentElse{#2}{}{\\Kaldenavn: & #3 &&}%
        \end{tabularx}
}

%Arguments
%   1) The argument to test
%   2) If empty then do this
%   3) If not empty then do this
\newcommand{\ifNoArgumentElse}[3]{
  \ifthenelse{\isempty{#1}}{#2}{#3}%
}

\newcommand{\cross}[1][1pt]{\ooalign{%
  \rule[1ex]{1ex}{#1}\cr% Horizontal bar
  \hss\rule{#1}{.7em}\hss\cr}% Vertical bar
}

\begin{document}

\subsection{Else Jensen}
\label{person1}

\createPerson
    {\nameref{person1}}
    {Else Nielsen}
    {Else}
    {11}
    {9}
    {1940}
    {13}
    {11}
    {1999}


\subsection{Hans Wright}
\label{person2}

\createPerson
    {\nameref{person2}}
    {}
    {Hans}
    {11}
    {9}
    {1940}
    {}
    {11}
    {1999}

\end{document}
musicman
  • 3,559
  • Thanks it works. Can you explain me, when do you have to use % and when doesn't it matter? Thanks :) – 7heViking Oct 13 '15 at 09:09
  • I don't know of any general rule. Personally I use more than necessary, I guess. You could ask a new question about that. Would be interesting to know. – musicman Oct 13 '15 at 09:28
  • I have created a question here: http://tex.stackexchange.com/questions/272865/when-to-use-and-when-to-not – 7heViking Oct 13 '15 at 23:12