1

I'm trying to create a big table using Latex. The result must be something like this: enter image description here

but what I get it's something like this: enter image description here

\documentclass{article}

\usepackage{tabularx}
\usepackage{adjustbox}
\usepackage{siunitx}
\usepackage{array}
\usepackage{graphicx}

\begin{document}

\begin{center}
    \begin{adjustbox}{width=20cm, center}
    \begin{tabular}{|c|m{0.5cm}|c|c|c|c|c|c|}
        \hline
        \textbf{Barra} & \textbf{Comprimento[$\si{\metre}$]} & \hspace{0pt}\textbf{Área da Seção[$\si{mm^2}$]} & \textbf{Força sobre a barra[$\si{\newton}$]} & \textbf{Tensão Normal[$\si{\mega\pascal}$]} & \hspace{0pt}\textbf{Módulo de Elasticidade[$\si{\mega\pascal}$]} & $\si{\epsilon_{L}}$ & \hspace{0pt}\textbf{Deformação[$\si{\metre}$]}\\
        \hline
        & & & & & & & \\
        \hline
        & & & & & & &\\
        \hline
        & & & & & & &\\
        \hline
        & & & & & & &\\
        \hline
        & & & & & & &\\
        \hline
        & & & & & & &\\
        \hline
        & & & & & & &\\
        \hline
        & & & & & & &\\
        \hline
        & & & & & & &\\
        \hline
    \end{tabular}
    \end{adjustbox}
\end{center}

\end{document}

Using this code I could only make the text smaller. I just can't wrap single words (big ones like "Deformação" and "Comprimento") in a cell and I already tried the \hspace{0pt} trick, but it doesn't seem to work. After trying solutions with packages like tabularx I'm still not able to make it work properly. Also, I could only make it stay centered using adjustbox. \centering didn't work. Can someone help me?

  • Have you tried adding \hyphenation in your preamble or discretionary hyphens in-line? I think that should help, along with setting the column width. – Holden Rohrer Aug 8 at 2:33
  • Either use p type columns instead if c type columns or uuse the makecell package that allows for manual linebreaks in table cells. – leandriis Aug 8 at 4:29
  • Also, don't use adjustbox as this will make font sizes inconsistent and sometimes illegibly small. Lastly you might want to take a look at the booktabs package. – leandriis Aug 8 at 4:31
  • To expand on @leandriis's suggestion, with makecell, you can use manual line breaks in column heads, \thead{Like\\this\\and that} (\thead creates a one-column tabular). – frougon Aug 8 at 7:45
4

It is just a matter using correct packages.

Use tabulary to get automatically width of columns, babel with correct lanuage (Portuguese was my best guess) and ragged2e with option [overload] to activate hyphenation in the headings. You have to help the hyphenation with \- where babel misses. In addition, the tabular is so wide, that you need to reduce the fontsize to footnotesize. Still, you either have to use landscape mode or reduce the left and right margins. I used geometry to reduce all margins.

The command \noindent before the tabular, avoids that the tabular moves to the left and protrude into the right margin. Also, I used an ordinary c column in the first two tabulars, because tabulary ended up with a too narrow first column (I could have adjusted the tymin).

You may consider using booktabs to get better spacing, but then you have to remove all vertical rules. I have included code using booktabs and also suggest that you reduce the number of horizontal rules, in addition to remove all the vertical ones.

If your are not willing or allowed to do something with the rules, use

\renewcommand{\arraystretch}{1.1}  % or 1.5
\setlength{\extrarowheight}{1pt}   % or 2 in case of 1.5 for arraystretch

to improve spacing.

If you want all columns to be of same width, use tabularx and eight similar columns. It seems that you want centred columns, so I have centred all of them by defining a new column type Y with the command:

\newcolumntype{Y}{>{\Centering\arraybackslash}X}

Centering is a command from the ragged2e package. You may define left or right aligned columns the same way:

\newcolumntype{W}{>{\RaggedRight\arraybackslash}X}
\newcolumntype{Z}{>{\RaggedLeft\arraybackslash}X}

Since I use the [overload] option, ragged2e will kick in even if you use \centering, \raggedright or \raggedleft. I have also increased the spacing considerable, as you indicated in the question.

If you are producing a scorecard to be filled out by hand with a pencil, the third tabular is OK. If you are typesetting a tabular for a presentation or document, you should (my opinion) use one of the booktabs versions.

If you give information regarding what will go into the tabular, you may get additional recommendation.

![enter image description here enter image description here

\documentclass{article}
\usepackage[portuguese]{babel}
\usepackage[margin=1.5cm]{geometry}
\usepackage{tabularx,tabulary, booktabs}
\usepackage{siunitx}
\usepackage[overload]{ragged2e}

\setlength{\tabcolsep}{2pt}
\renewcommand{\arraystretch}{1.1}
\setlength{\extrarowheight}{1pt}

\begin{document}

\section*{tabulary}


\footnotesize
{
\noindent\begin{tabulary}{\linewidth}{|c|C|C|C|C|C|C|C|}
\hline
        \textbf{Barra} & \textbf{Compri\-mento[$\si{\metre}$]} & \hspace{0pt}\textbf{Área da Seção[$\si{mm^2}$]} & \textbf{Força sobre a barra[$\si{\newton}$]} & \textbf{Tensão Normal[$\si{\mega\pascal}$]} & \hspace{0pt}\textbf{Módulo de Elasticidade[$\si{\mega\pascal}$]} & $\si{\epsilon_{L}}$ & \hspace{0pt}\textbf{Defor\-mação[$\si{\metre}$]}\\
        \hline
        & & & & & & & \\
        \hline
        & & & & & & &\\
        \hline
        & & & & & & &\\
        \hline
        & & & & & & &\\
        \hline
        & & & & & & &\\
        \hline
        & & & & & & &\\
        \hline
        & & & & & & &\\
        \hline
        & & & & & & &\\
        \hline
        & & & & & & &\\
        \hline
    \end{tabulary}%
}

\vspace{2cm}
{
\noindent\begin{tabulary}{\linewidth}{@{}c*{7}{C}@{}}
\toprule
        \textbf{Barra} & \textbf{Compri\-mento[$\si{\metre}$]} & \hspace{0pt}\textbf{Área da Seção[$\si{mm^2}$]} & \textbf{Força sobre a barra[$\si{\newton}$]} & \textbf{Tensão Normal[$\si{\mega\pascal}$]} & \hspace{0pt}\textbf{Módulo de Elasticidade[$\si{\mega\pascal}$]} & $\si{\epsilon_{L}}$ & \textbf{Defor\-mação[$\si{\metre}$]}\\
\midrule
        1 & 20 & 300 & 400 & 500 & 600 & 7 & 800\\
        2 & 20 & 300 & 400 & 500 & 600 & 7 & 800\\
        3 & 20 & 300 & 400 & 500 & 600 & 7 & 800\\
        4 & 20 & 300 & 400 & 500 & 600 & 7 & 800\\
        5 & 20 & 300 & 400 & 500 & 600 & 7 & 800\\\midrule
        6 & 20 & 300 & 400 & 500 & 600 & 7 & 800\\
        7 & 20 & 300 & 400 & 500 & 600 & 7 & 800\\
        8 & 20 & 300 & 400 & 500 & 600 & 7 & 800\\
        9 & 20 & 300 & 400 & 500 & 600 & 7 & 800\\
        10 & 20 & 300 & 400 & 500 & 600 & 7 & 800\\
    \bottomrule
    \end{tabulary}
\pagebreak
\section*{tabularx}
}
{
\renewcommand{\arraystretch}{1.5}
\setlength{\extrarowheight}{2pt}
\setlength{\arrayrulewidth}{0.2pt}
\newcolumntype{Y}{>{\Centering\arraybackslash}X}

\noindent\begin{tabularx}{\linewidth}{*{8}{|Y}|}
\hline
        \textbf{Barra} & \textbf{Compri\-mento[$\si{\metre}$]} & \hspace{0pt}\textbf{Área da Seção[$\si{mm^2}$]} & \textbf{Força sobre a barra[$\si{\newton}$]} & \textbf{Tensão Normal[$\si{\mega\pascal}$]} & \hspace{0pt}\textbf{Módulo de Elasti\-cidade[$\si{\mega\pascal}$]} & $\si{\epsilon_{L}}$ & \textbf{Defor\-mação[$\si{\metre}$]}\\
\hline
        1 & 20 & 300 & 400 & 500 & 600 & 7 & 800\\\hline
        2 & 20 & 300 & 400 & 500 & 600 & 7 & 800\\\hline
        3 & 20 & 300 & 400 & 500 & 600 & 7 & 800\\\hline
        4 & 20 & 300 & 400 & 500 & 600 & 7 & 800\\\hline
        5 & 20 & 300 & 400 & 500 & 600 & 7 & 800\\\hline
        6 & 20 & 300 & 400 & 500 & 600 & 7 & 800\\\hline
        7 & 20 & 300 & 400 & 500 & 600 & 7 & 800\\\hline
        8 & 20 & 300 & 400 & 500 & 600 & 7 & 800\\\hline
        9 & 20 & 300 & 400 & 500 & 600 & 7 & 800\\\hline
        10 & 20 & 300 & 400 & 500 & 600 & 7 & 800\\
    \hline
    \end{tabularx}%
}

\vspace{2cm}

{
\newcolumntype{Y}{>{\Centering\arraybackslash}X}

\noindent\begin{tabularx}{\linewidth}{*{8}{Y}}
\toprule
        \textbf{Barra} &
        \textbf{Compri\-mento[$\si{\metre}$]}l &
        \hspace{0pt}\textbf{Área da Seção[$\si{mm^2}$]} &
        \textbf{Força sobre a barra[$\si{\newton}$]} &
        \textbf{Tensão Normal[$\si{\mega\pascal}$]} &
        \textbf{Módulo de Elasti\-cidade[$\si{\mega\pascal}$]} &
        $\si{\epsilon_{L}}$ &
        \textbf{Defor\-mação[$\si{\metre}$]}\\
\midrule
        100 & 20 & 300 & 400 & 500 & 600 & 700 & 800\\
        200 & 20 & 300 & 400 & 500 & 600 & 700 & 800\\
        300 & 20 & 300 & 400 & 500 & 600 & 700 & 800\\
        400 & 20 & 300 & 400 & 500 & 600 & 700 & 800\\
        500 & 20 & 300 & 400 & 500 & 600 & 700 & 800\\\addlinespace[6pt]
        600 & 20 & 300 & 400 & 500 & 600 & 700 & 800\\
        700 & 20 & 300 & 400 & 500 & 600 & 700 & 800\\
        800 & 20 & 300 & 400 & 500 & 600 & 700 & 800\\
        900 & 20 & 300 & 400 & 500 & 600 & 700 & 800\\
        1000 & 20 & 300 & 400 & 500 & 600 & 700 & 800\\
    \hline
    \end{tabularx}%
}
\end{document}
2

As supplement/alternative to @Sveinung second example in his answer:

  • I suspect, that in column are numbers. Unfortunately they are unknown. Do they are all integers? Do they have all the same number of digits? Anyway, I would rather use S column types and consider @frougon comment below question:
\documentclass{article}
%\usepackage[portuguese]{babel}
\usepackage{geometry}
\usepackage{siunitx}
\usepackage{booktabs, makecell}
\renewcommand\theadfont{\footnotesize\bfseries}
\renewcommand\theadgape{}

%---------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%

\begin{document}
    \begin{center}
\small
\setlength{\tabcolsep}{4pt}        % Reduce space between columns
\setcellgapes{3pt}
\makegapedcells
\begin{tabular}{*{8}{S}}
    \toprule
{\thead{Barra}}
    &   {\thead{Compri-\\mento [\si{\metre}]}}         
        &   {\thead{Área da\\ Seção [\si{mm^2}]}}
    &   {\thead{Força sobre\\ a barra [\si{\newton}]}}
        & {\thead{Tensão\\ Normal [\si{\mega\pascal}]}}
    &   {\thead{Módulo de\\ Elasticidade [\si{\mega\pascal}]}} 
        &   {\si{\epsilon_{L}}}
    & {\thead{Deformação\\ {[\si{\metre}]}}}  \\
    \midrule
1  & 12.3 & 30 & 40      & 55.7  & 60 & 7 & 8  \\
12 &  4.5 & 33 & 38,2    & 33.44 & 60 & 7 & 6  \\
    \bottomrule
    \end{tabular}
\end{center}
\end{document}
  • For S column type I didn't determine format of numbers, for example S[table-format=2,2] since numbers are unknown, however use default values seems to give quite good result:

enter image description here

  • As you can see, I remove from column headers all \hspace{...} (it is not clear what is their functions) and remove math environment for units, i.e. instead [$\si{metre}$] is used simple [\si{\metre}].

  • At the last column units is inserted in curly braces: \thead{Deformação\\ {[\si{\metre}]}}. There this is necessary since otherwise compilation result in unexpected error.

  • The \hspace{0pt} is used to get the first word in a narrow column to hyphenate if necessary. It is unnecessary when you use ragged2e, but necessary if you do not and have a column where the first word needs to be hyphenated. Column 2 and 8 are candidates here. – Sveinung Aug 8 at 16:24
  • In the text book used at school teaching typography in Norway, they recommended as good typography to normalise all figures, i.e. add zeros to have the same number of decimal. In addition, all column with figures should be set right aligned. Also, they recommended that header should be typeset with one grad smaller fonts, and not use emphasising. Of course, right aligned columns is a problem when the header text are as long as in this example. – Sveinung Aug 8 at 17:05
  • @Sveinung, i agree with your information in above comment. Personally I usually use S columns (from siunitx) with numbers with options round-mode and round-precision. Also I put a lot effort to make columns header short and clear (what many cases is not simply). I saw the last book in my native language about typography when books were printed with presses machines (using lead for letters) :-(. – Zarko Aug 8 at 17:40

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.