像表格一样对齐文本

像表格一样对齐文本

我想要实现的是: 在此处输入图片描述
我定义了一个这样的新命令:

\newcommand{\details}[2]{
#1 \hspace{2cm} : #2 \\
}

完整的 MWE 如下:

\documentclass[12pt]{article}
\newcommand{\details}[2]{
#1 \hspace{2cm} : #2 \\
}
\begin{document}
\noindent
\details{Name}{Subham Soni}
\details{Date of Birth}{March 22, 1994}
\details{Nationality}{Indian}
\details{Address for Communication}{XXXYYY}
\end{document}

我得到的是:

在此处输入图片描述
现在,有两件事:
1)即使添加后\noindent,也会有轻微的缩进
2)冒号没有对齐。

我希望命令执行的操作是,无论文本是什么(姓名、出生日期等),在其后留出 2 厘米,然后打印冒号。此外,如果文本长度为 2,则文本必须断开并仅出现在冒号后

我该怎么做?

答案1

您可以使用tabularJubobs 提到的。这是试图纠正您的方法。您在第一个单词后添加了 2cm 的水平空间。然后将:放置在第一个单词右侧 2cm 处。这就是它们未垂直对齐的原因。补救措施是将第一个单词放在某个长度的框内,然后放置:。您实际上可以计算最宽的第一个单词的宽度并设置框的宽度。

\documentclass[12pt]{article}
\usepackage{calc}
\newlength{\mylen}
\setlength{\mylen}{\widthof{Address for Communication}}
\newcommand{\details}[2]{%
\makebox[\mylen][l]{#1} \,: #2 \\
}
\begin{document}
\noindent
\details{Name}{Subham Soni}
\details{Date of Birth}{March 22, 1994}
\details{Nationality}{Indian}
\details{Address for Communication}{XXXYYY}
\end{document}

在此处输入图片描述

以下是使用 s 的解决方案minipage

\documentclass[12pt,draft]{article}
\usepackage{calc,showframe}
\newlength{\mylen}
\setlength{\mylen}{\widthof{Address for Communication}}
\newlength{\mylength}
\setlength{\mylength}{\widthof{:}}
\newcommand{\details}[2]{%
\begin{minipage}[t]{\mylen}
#1
\end{minipage}%
\hspace{2mm}
\begin{minipage}[t]{\dimexpr\mylength+2mm}
:
\end{minipage}%
\begin{minipage}[t]{\dimexpr\textwidth-6mm-\mylen-\mylength\relax}
 #2 \par
\end{minipage}
\par\noindent
\ignorespaces
}
\begin{document}
\noindent
\details{Name}{Subham Soni}
\details{Date of Birth}{March 22, 1994}
\details{Nationality}{Indian}
\details{Address for Communication}{This is going to be some long address This is going to be some long address}
\end{document}

这是一个tabular解决方案(改进的 Jubobss 解决方案)

\documentclass[12pt]{article}
\usepackage{calc,array}
\newlength{\mylen}
\setlength{\mylen}{\widthof{Address for Communication}}
\newcommand{\details}[2]{%
\begin{tabular}[t]{@{}p{\mylen}!{:}p{\dimexpr\textwidth-\mylen-4\tabcolsep\relax}}
#1 & #2 
\end{tabular}%
\par\noindent
\ignorespaces
}
\begin{document}
\noindent
\details{Name}{Subham Soni}
\details{Date of Birth}{March 22, 1994}
\details{Nationality}{Indian}
\details{Address for Communication}{This is going to be some long address This is going to be some long address}
\end{document}

这个使用tabularx

\documentclass[12pt]{article}
\usepackage{array,tabularx}
\newcommand{\details}[2]{%
#1 & #2  \\
}
\begin{document}
\noindent
\begin{tabularx}{\textwidth}[t]{@{}>{\bfseries}l!{:}X}
\details{Name}{Subham Soni}
\details{Date of Birth}{March 22, 1994}
\details{Nationality}{Indian}
\details{Address for Communication}{This is going to be some long address This is going to be some long address}
\end{tabularx}%
\end{document}

在此处输入图片描述

答案2

\details“轻微缩进”是由于的定义开头的虚假空格造成的。

如果您想要tabular类似输出,为什么不使用tabular环境?

在此处输入图片描述

\documentclass[12pt]{article}

\newcommand*{\details}[2]{% <--- there was a spurious space here
  \textbf{#1} & : #2 \\
}

\begin{document}
\begin{tabular}{l@{\hspace{2cm}}l}
\details{Name}{Subham Soni}
\details{Date of Birth}{March 22, 1994}
\details{Nationality}{Indian}
\details{Address for Communication}{XXXYYY}
\end{tabular}
\end{document}

答案3

如果您愿意,parboxes可以改为tabulars。无论如何,我认为tabular更好,因为它会自动计算列宽,而在这种情况下,您需要知道最大的行。

\documentclass[12pt]{article}

\newcommand{\details}[2]{%
\parbox[t]{3cm}{\raggedright \strut#1\strut} : \parbox[t]{3cm}{\raggedright \strut#2\strut}\\
}

\begin{document}
\noindent
\details{Name}{Subham Soni and some more text to see what happens}
\details{Date of Birth}{March 22, 1994}
\details{Nationality}{Indian}
\details{Address for Communication}{XXXYYY and some more text}
\end{document}

在此处输入图片描述

答案4

你也可以使用\parbox。代码如下:

\documentclass[12pt]{article}
\newcommand{\details}[2]{\noindent\strut%
  \parbox[t]{.48\textwidth}{\raggedright\strut#1\strut}\ :\ % add 2cm if you wish
  \parbox[t]{.5\textwidth}{\raggedright\strut#2\strut}\par}
\begin{document}
\pagestyle{empty}
\details{Name}{Subham Soni}
\details{Date of Birth}{March 22, 1994}
\details{Nationality}{Indian}
\details{Address for Communication}{XXXYYY}
\end{document}

输出

在此处输入图片描述

相关内容