Search code examples
latex

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

enter image description here

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


Solution

  • Your 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}
    

    enter image description here