简体   繁体   中英

Using minipage in tabular LATEX

I want to use the minipage in a tabular, but the alignment of the contents are not the same. I want to have the first input top right and the second input be left align. Code I used is:

\newlength{\smallertextwidth}
\setlength{\smallertextwidth}{\textwidth}
\addtolength{\smallertextwidth}{-2cm}

\usepackage{array}
\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}p{#1}}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}p{#1}}
\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}p{#1}}


\newcommand{\mytabb}[2]{
\begin{tabular}{R{1.5cm}L{1cm}} 
\textbf{#1} & 
\begin{minipage}{\smallertextwidth}
#2
\end{minipage} 
\end{tabular}
}

The output using this code is:

\mytabb{YEAR}{This is a long text. This is a long text.This is a long text.This is a long text.This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. \newline This is a long text. This is a long text.This is a long text.This is a long text. This is a long text.This is a long text. This is a long text.}

在此处输入图像描述

what should I do to have the YEAR on the same line to the text on next column?

You minipage must not be wider than the column, otherwise a line break will be inserted before that. In your code the minipage is the width of the whole text plus 1cm, but your column is only 1 cm.

To avoid the problem:

  1. make the column much wider
  2. use \linewidth for the minipage, because in contrast to textwidth, this will adapt to the space available in the column.

\documentclass{article}

\usepackage{array}
\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}p{#1}}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}p{#1}}
\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}p{#1}}


\newcommand{\mytabb}[2]{
\begin{tabular}{R{1.5cm}L{9cm}} 
\textbf{#1} &\begin{minipage}[t]{\linewidth}
#2
\end{minipage} 
\end{tabular}
}

\begin{document}

\mytabb{YEAR}{This is a long text. This is a long text.This is a long text.This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. \newline This is a long text. This is a long text.This is a long text.This is a long text. This is a long text.This is a long text. This is a long text.}

\end{document}

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM