我有一个有 4 列和 2 2-3 行的 tabularx。问题是,尽管第一行的行为符合预期,但第二行却没有。我试图将第 3 列设置为右对齐,将第 4 列设置为居中。但是,我无法让它在所有行上工作。我创建了一个最小示例,它显示了我的设置和我的问题。
在示例代码中,我创建了两个表,只是为了显示差异。
\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:}& \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
如果我理解正确的话,你的问题很简单,你必须在行尾使用 %,并注意任何地方都没有不必要的空格。
\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:} & \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}