简体   繁体   English

在表格 LATEX 中使用 minipage

[英]Using minipage in tabular LATEX

I want to use the minipage in a tabular, but the alignment of the contents are not the same.我想在表格中使用minipage,但是alignment的内容不一样。 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:使用此代码的 output 是:

\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?我应该怎么做才能让 YEAR 与下一列的文本在同一行?

You minipage must not be wider than the column, otherwise a line break will be inserted before that.您的 minipage 不能比列宽,否则会在此之前插入换行符。 In your code the minipage is the width of the whole text plus 1cm, but your column is only 1 cm.在您的代码中,minipage 是整个文本的宽度加上 1 厘米,但您的列只有 1 厘米。

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.对 minipage 使用\linewidth ,因为与 textwidth 相比,这将适应列中的可用空间。

\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}

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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