0

I have a simple table:

    \begin{table}[]

        \begin{tabular}{c|c|c|c|c|c|c|c|}
            \cline{2-7} & 
            \multicolumn{1}{p{2.7cm}|}{RIGHT} & 
            \multicolumn{1}{c|}{LEFT}&   
            \multicolumn{1}{c|}{LEFT}&  
            \multicolumn{1}{c|}{LEFT}&
            \multicolumn{1}{c|}{LEFT}& 
            \multicolumn{1}{c|}{LEFT} \\ \hline

            \multicolumn{1}{|c|}{test1} & 1 & 1 &1 & 1 & 1 &1\\ \hline
            \multicolumn{1}{|c|}{test2} & 1 & 1 &1 & 1 & 1 &1\\ \hline
            \multicolumn{1}{|c|}{test2} & 1 & 1 &1 & 1 & 1 &1\\ \hline
        \end{tabular}

\end{table}

Looks like this:

enter image description here

I am trying to make this table to fit in the page and to increase the row size when text is too long but I didn't manage to do that.

In the above table if in a cell I insert a long text it will look like this: enter image description here

Someone help me to resize my table with this:

\newcommand*{\ResizeTableIfTooBig}[1]{%
%% #1 = table
\savebox{\MyTable}{#1}%
\ifdim\wd\MyTable<\linewidth
\usebox{\MyTable}%
\else
\resizebox{\textwidth}{!}{\usebox{\MyTable}}%
\fi
}%

The problem is that this snapshot just scales the table and when text from cells is too long the font is very small.

What will help me is a table with 7 columns which will not come out of the paper and will create a new line when the text is too long for each column(without scaling font). Something like this: enter image description here

  • 2
    Don't use \resizebox on tables. – Skillmon Oct 5 '18 at 10:55
  • Then how can I fix this? – Adrian Oct 5 '18 at 11:16
  • @samcarter sorry. I forgot about this. Done :) – Adrian Dec 17 '18 at 16:31
3

You can use tabularx to make your table fit line width:

\documentclass{article}
\usepackage{tabularx, lipsum}
\setlength{\extrarowheight}{2pt}

\begin{document}

\lipsum[11]
\begin{table}[!htb]
    \begin{tabularx}{\linewidth}{|c|>{\centering}p{2.7cm}|*{5}{>{\centering\arraybackslash}X|}}
        \cline{2-7}
      \multicolumn{1}{c|}{} & RIGHT & LEFT & LEFT & LEFT & LEFT & LEFT \\ 
       \hline
       test1 & 1 & 1 &1 & 1 & 1 & I vel II vel III vel \&c. \\ \hline
       test2 & 1 & 1 &1 & 1 & 1 &1 \\ \hline
       test2 & 1 & 1 &1 & 1 & 1 &1 \\ \hline
    \end{tabularx}
\end{table}

\end{document} 

enter image description here

1

Would that work?

\documentclass{article}
\usepackage{array}

\begin{document}

        \begin{tabular}{c|c|c|c|c|c|c|c|}
            \cline{2-7} &
            \multicolumn{1}{p{2.7cm}|}{RIGHT} &
            \multicolumn{1}{ >{\raggedright}p{2.5cm}<{} | }{LEFT LEFT LEFT LEFT LEFT LEFT LEFT LEFT }&
            \multicolumn{1}{c|}{LEFT}&
            \multicolumn{1}{c|}{LEFT}&
            \multicolumn{1}{c|}{LEFT}&
            \multicolumn{1}{c|}{LEFT} \\ \hline

            \multicolumn{1}{|c|}{test1} & 1 & 1 &1 & 1 & 1 &1\\ \hline
            \multicolumn{1}{|c|}{test2} & 1 & 1 &1 & 1 & 1 &1\\ \hline
            \multicolumn{1}{|c|}{test2} & 1 & 1 &1 & 1 & 1 &1\\ \hline
        \end{tabular}

\end{document}

enter image description here

Edit: It looks like you want the same behaviour in each cell of the table, and not only in some header cells, as I first thought. In this case, the following is probably more convenient:

\documentclass{article}
\usepackage{array}

\newcolumntype{C}{>{\raggedright\arraybackslash}p{1.5cm}<{}}

\begin{document}

\begin{tabular}{C|C|C|C|C|C|C}
1 & 2 & 3 & 4 & 5 & 6 & 7 \\ \hline
1 & 2 & LEFT LEFT LEFT LEFT LEFT LEFT LEFT LEFT & 4 & 5 & 6 & 7 test test test tes tt test  \\
\end{tabular}

\end{document}

enter image description here

  • It looks nice but if I add in the last right cell some more text it goes out of the page. (imgur.com/a/lzYGLgy) – Adrian Oct 5 '18 at 12:39
  • of course it does. You need to use the >{\raggedright}p{2.5cm}<{} | style for each column you want to enter things. – T. Pluess Oct 5 '18 at 12:41
  • here is what I have changed: pastebin.com/anvbMq1h . I am doing something wrong because the cell increases his size and goes out of the page. – Adrian Oct 5 '18 at 13:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.