1

I'm starting my learning in LaTeX at university and I need to insert the tables below in my text, but they escape the margins. Is there an automatic command in LaTeX that adjusts the size of columns and font so that the table fits within the text margins?

I don't know much about the codes, practically everything you are seeing I took from other sites. Thanks in advance.

\begin{table}[!ht]
\centering
\caption{Representação}
\begin{tabular}{c | c c c c c c c c c c c c c}
\textbf{} & \textbf{brócolis} & \textbf{com} & \textbf{comidas} & \textbf{ela} & \textbf{ele} & \textbf{fez} & \textbf{gosta} & \textbf{milho} & \textbf{palmito} & \textbf{pizza} & \textbf{salada} & \textbf{são} & \textbf{vapor} \\
\hline
$d_1$  & 1 & 0 & 0 & 1 & 1 & 0 & 1 & 1 & 0 & 1 & 0 & 0 & 0 \\
$d_2$  & 1 & 0 & 1 & 0 & 0 & 0 & 0 & 1 & 0 & 1 & 0 & 1 & 0 \\
$d_3$  & 1 & 1 & 0 & 0 & 1 & 1 & 0 & 1 & 1 & 0 & 1 & 0 & 0 \\
$d_4$  & 1 & 1 & 0 & 1 & 0 & 0 & 1 & 1 & 0 & 0 & 0 & 0 & 1 \\
\end{tabular}
\label{tab:boolean}
\end{table}

\begin{table}[!ht] \centering \caption{Representação} \begin{tabular}{c | c c c c c c c c c c c c c} \textbf{} & \textbf{brócolis} & \textbf{com} & \textbf{comidas} & \textbf{ela} & \textbf{ele} & \textbf{fez} & \textbf{gosta} & \textbf{milho} & \textbf{palmito} & \textbf{pizza} & \textbf{salada} & \textbf{são} & \textbf{vapor} \ \hline $d_1$ & 0,000 & 0,000 & 0,000 & 0,301 & 0,301 & 0,000 & 0,602 & 0,000 & 0,000 & 0,602 & 0,000 & 0,000 & 0,000 \ $d_2$ & 0,000 & 0,000 & 0,602 & 0,000 & 0,000 & 0,000 & 0,000 & 0,000 & 0,000 & 0,301 & 0,000 & 0,602 & 0,000 \ $d_3$ & 0,000 & 0,301 & 0,000 & 0,000 & 0,301 & 1,204 & 0,000 & 0,000 & 0,602 & 0,000 & 1,806 & 0,000 & 0,000 \ $d_4$ & 0,000 & 0,301 & 0,000 & 0,301 & 0,000 & 0,000 & 0,301 & 0,000 & 0,000 & 0,000 & 0,000 & 0,000 & 0,602 \ \end{tabular} \label{tab:tfidf} \end{table}

Zarko
  • 296,517
Tarugo
  • 13
  • 1
    unrelated but don't routinely use [!ht] it might be useful to over-ride the defaults in special situations, but it disables much of latex's float placement algorithm. – David Carlisle Sep 03 '22 at 07:47

2 Answers2

2

Welcome to TeX.SE!

  • Writing tables is challenging task even for experienced LaTeX users.

  • For beginners in LaTeX is helpful to read some introductory text about writing of tables, for example wiki/LaTeX/Tables.

  • In your case see question, it seems that your question is duplicate to it.

  • Possible solutions for tables shown in your question also depends on page layout. In lack of this information, is hard to help you. So, please always provide (if possible) a complete small document beginning with \documentclass and ending with \end{document} called Minimal Working example (MWE), which reproduce your problem.

  • There is no general solution, Exist many possible solutions depends od table contents, page layout etc. Promising way is use of tables, to which you can prescribe table width.

  • Solutions based on use of a \resizebox is desperate measure of last resort to fit table in column/page width. It make font size in table inconsistent with fonts in text which in the worst become illegibly small.

  • Among of possible solutions, in your particular case, see if the following can help you:

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

\begin{document} \begin{table}[ht] \centering \caption{Representação} \small \setlength\tabcolsep{0pt} \begin{tabular}{\linewidth}{@{\extracolsep{\fill}} c @{\ } | c c c c c c c c c c c c c} \textbf{} & \textbf{brócolis} & \textbf{com} & \textbf{comidas} & \textbf{ela} & \textbf{ele} & \textbf{fez} & \textbf{gosta} & \textbf{milho} & \textbf{palmito} & \textbf{pizza} & \textbf{salada} & \textbf{são} & \textbf{vapor} \ \hline $d_1$ & 1 & 0 & 0 & 1 & 1 & 0 & 1 & 1 & 0 & 1 & 0 & 0 & 0 \ $d_2$ & 1 & 0 & 1 & 0 & 0 & 0 & 0 & 1 & 0 & 1 & 0 & 1 & 0 \ $d_3$ & 1 & 1 & 0 & 0 & 1 & 1 & 0 & 1 & 1 & 0 & 1 & 0 & 0 \ $d_4$ & 1 & 1 & 0 & 1 & 0 & 0 & 1 & 1 & 0 & 0 & 0 & 0 & 1 \ \end{tabular} \label{tab:boolean} \end{table}

\begin{table}[!ht] \centering \centering \caption{Representação} \small \setlength\tabcolsep{0pt} \begin{tabular}{\linewidth}{@{\extracolsep{\fill}} c @{\ } | c c c c c c c c c c c c c} \textbf{} & \textbf{brócolis} & \textbf{com} & \textbf{comidas} & \textbf{ela} & \textbf{ele} & \textbf{fez} & \textbf{gosta} & \textbf{milho} & \textbf{palmito} & \textbf{pizza} & \textbf{salada} & \textbf{são} & \textbf{vapor} \ \hline $d_1$ & 0,000 & 0,000 & 0,000 & 0,301 & 0,301 & 0,000 & 0,602 & 0,000 & 0,000 & 0,602 & 0,000 & 0,000 & 0,000 \ $d_2$ & 0,000 & 0,000 & 0,602 & 0,000 & 0,000 & 0,000 & 0,000 & 0,000 & 0,000 & 0,301 & 0,000 & 0,602 & 0,000 \ $d_3$ & 0,000 & 0,301 & 0,000 & 0,000 & 0,301 & 1,204 & 0,000 & 0,000 & 0,602 & 0,000 & 1,806 & 0,000 & 0,000 \ $d_4$ & 0,000 & 0,301 & 0,000 & 0,301 & 0,000 & 0,000 & 0,301 & 0,000 & 0,000 & 0,000 & 0,000 & 0,000 & 0,602 \ \end{tabular} \label{tab:tfidf} \end{table} \end{document}

enter image description here

(red lines indicate page layout)

Zarko
  • 296,517
0

I see two options:

In the first option, load the package tabularx and since you have centered columns you have to create a new type of column, like this:

\usepackage{tabularx}
\newcolumntype{Y}{>{\centering\arraybackslash}X}

then on the tabular environment use the following code

\begin{tabularx}{\textwidth}{Y | Y Y Y Y Y Y Y Y Y Y Y Y Y}
<Insert Table Content here>
\end{tabularx}

The other option is to wrap the tabular environment with the \resizebox command as this:

\resizebox{\textwidth}{!}{
<insert tabular enviroment here>
}

The advantage to the last one is that it also tries to change the height of the text

  • 2
    please never suggest using resizebox on tables, it produces inconsistent font sizes and rule widths You should use a named font size such as \small not an arbitrary scaling – David Carlisle Sep 03 '22 at 07:50