简体   繁体   中英

Fit table size with the page size in latex

I have many tables with different numbers of columns that should fit with the page size. For that, I used codes similar to the one below:

\begin{table}
    \caption{Values}
    \centering
    \resizebox{\linewidth}{!}{
        \begin{tabular}{!{\vrule}l!{\vrule}l!{\vrule}l!{\vrule}l!{\vrule}l!{\vrule}l} 
            \toprule
            DS             & Criterion1 & Criterion2 & Criterion3 & Criterion4  \\ 
            \midrule
            DS1 & V1 & V2 & V3  & V4 \\ 
            \midrule
            DS2       & V5 & V6 & V7 & V8 \\ 
            \midrule
            DS3       & V9 & V10 & V11 & V12 \\ 
            \midrule
            DS4 & V13 & V14 & V15 & V16 \\
            \botrule
        \end{tabular}
    }
\end{table}

This code generates the tables but shows errors of the kind: Illegal characters in array arg . I didn't understand this error.

Any help would be much appreciated.

My suggestion to edit your code:

\documentclass[10pt]{article}
\usepackage{tabularx,booktabs}

\begin{document}
\noindent
\begin{table}
  \caption{Values}
  \label{tab:n}
  \centering
  \begin{tabularx}{\textwidth}{*{5}{X}}
    \toprule
    DS  & Criterion1 & Criterion2 & Criterion3 & Criterion4 \\ 
    \midrule
    DS1 & V1         & V2         & V3         & V4 \\ 
    \midrule
    DS2 & V5         & V6         & V7         & V8 \\ 
    \midrule
    DS3 & V9         & V10        & V11        & V12 \\ 
    \midrule
    DS4 & V13        & V14        & V15        & V16 \\
    \bottomrule
  \end{tabularx}
\end{table}
\end{document}

输出截图

  1. Not related, but you should have a command \\label{...} after your \\caption{...} .

  2. Do not mix vertical lines (best of all avoid them) with the horizontal ones from the package booktabs ( toprule , midrule , bottomrule ).

  3. And this should hopefully answer your question: try column ytpe X from the package tabularx to better stretch the width of columns (I guess you have tables with more column than this one).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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