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}
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}
| improve this answer | |
  • 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 9: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 9:28
  • I have created a question here: tex.stackexchange.com/questions/272865/… – 7heViking Oct 13 '15 at 23:12

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.