使用 hfill 进行左对齐、右对齐和居中对齐

使用 hfill 进行左对齐、右对齐和居中对齐

这个问题让我抓狂。我需要一行格式如下的文本:

Avian Influenza       Tamiflu          Jan 2, 2011
* This is a random description, note that it spans 
  mult. cols.
* These bullet points--would like to use itemize
  for them.

Chicken pox           DrugXYZ          Feb 3, 1998
* This is another description.
* The "headers" (e.g. DrugXYZ, Avian Influenza) can  
be different, and have different lengths.

基本上,我希望Avian Influenza左对齐、Tamiflu居中和Jan 2, 2011右对齐,这样两组中的疾病、日期和药物名称都对齐在相同的标签上。总而言之,这三者应该跨越textwidth

我尝试使用表格,但我希望能够itemize在“内容”部分使用等。

有任何想法吗??

答案1

另一个可能的解决方案:

\documentclass{article}
\usepackage{array}

\newcommand\MyHeadings[3]{%
  \multicolumn{2}{@{}p{0.45\textwidth}@{}}{#1}  & #2 & #3 \tabularnewline
}
\newcommand\Description[1]{%
  \textbullet & \multicolumn{3}{@{}p{0.97\textwidth}@{}}{#1}\tabularnewline}

\begin{document}

\noindent
\begin{tabular}{@{}p{0.03\textwidth}@{}p{0.42\textwidth}@{}p{0.3\textwidth}@{}>{\raggedleft}p{0.25\textwidth}@{}}
\MyHeadings{Avian Influenza}{Tamiflu}{Jan 2, 2011}
\Description{stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff}
\Description{stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff}
\MyHeadings{Chicken pox}{DrugXYZ}{Feb 3, 1998}
\Description{stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff}
\Description{stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff}
\end{tabular}

\end{document}

还有另一个选项:tabular仅用于需要对齐的单词,描述留给环境itemize(借助 进行定制enumitem):

\documentclass{article}
\usepackage{enumitem}
\usepackage{array}

\setitemize{leftmargin=*,nolistsep}
\newcommand\MyHeadings[3]{%
  \noindent\begin{tabular}  {@{}p{0.45\textwidth}@{}p{0.3\textwidth}@{}>{\raggedleft}p{0.25\textwidth}@{}}
    #1 & #2 & #3
\end{tabular}\par
}

\begin{document}

\MyHeadings{Avian Influenza}{Tamiflu}{Jan 2, 2011}
\begin{itemize}
  \item stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff
  \item stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff
\end{itemize}
\MyHeadings{Chicken pox}{DrugXYZ}{Feb 3, 1998}
\begin{itemize}
  \item stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff
  \item stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff
\end{itemize}


\end{document}

答案2

简单的应用\hfil就足够了。例如,将事情包装在宏中,有什么问题

\documentclass{article}
\newcommand\threethings[3]{%
  \noindent #1\hfil #2\hfil #3\\\ignorespaces
}
\begin{document}

\threethings{SomeWords1}{SomeWords2}{SomeWords3}
stuff stuff stuff stuff stuff stuf
stuff stuff stuff stuff

\threethings{SomeWords1}{SomeWords2}{SomeWords3}
stuff stuff stuff stuff stuff stuf
stuff stuff stuff stuff
\end{document}

根据评论,第二种可能的方法

\documentclass{article}
\newcommand*\mylength{0.33343\textwidth}
\newcommand\threethings[3]{%
  \noindent
  \begin{tabular}{@{}p{\mylength}@{}p{\mylength}@{}p{\mylength}@{}}
   \raggedright #1 & \centering #2 & \raggedleft #3
  \end{tabular}
}
\begin{document}

\threethings{SomeWords1}{SomeWords2}{SomeWords3}
stuff stuff stuff stuff stuff stuff
stuff stuff stuff stuff

\threethings{SomeWords1}{SomeWords2}{SomeWords3}
stuff stuff stuff stuff stuff stuff
stuff stuff stuff stuff

\threethings{Short1}{Short2}{Short3}
stuff stuff stuff stuff stuff stuf
stuff stuff stuff stuff
\end{document}

答案3

您需要的对齐可以通过以下方式获得

\usepackage{enumitem}
\newenvironment{disease}[3]
  {\par\addvspace{\topsep}\noindent
   \makebox[0pt][l]{#1}\makebox[\textwidth]{#2}\makebox[0pt][r]{#3}
   \par\nobreak
   \begin{itemize}[topsep=0pt,beginpenalty=10000]}
  {\end{itemize}\addvspace{\topsep}}

\begin{document}

\begin{disease}{Avian Influenza}{Tamiflu}{Jan 2, 2011}
\item stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff
stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff
stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff

\item stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff
stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff
stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff stuff
\end{disease}

标题和逐项列表的第一行之间没有间断。

答案4

\documentclass{report}
\usepackage{tabularx}
\begin{document}

\noindent
\begin{tabularx}{\linewidth}{@{}p{0.333\linewidth}@{} >{\centering}X @{}
                  >{\raggedleft}p{0.333\linewidth}@{}}
SomeWords1  & SomeWords2  & SomeWords3\tabularnewline
stuff stuff & stuff stuff & stuff stuf\tabularnewline
stuff stuff & stuff stuff 
\end{tabularx}

\end{document}

在此处输入图片描述

相关内容