1

have a table like below with a custom width, but i can not run it, any ideas? many thanks in advance

\documentclass{article}
\usepackage{array}
\begin{document}
\begin{table}[ht]
    \centering
    \begin{tabular}{p{0.015\textwidth}|R{0.37\textwidth}|R{0.12\textwidth}|R{0.08\textwidth}|R{0.02\textwidth}|p{0.35\textwidth}|}
        \hline
        & Sepal.Length & Sepal.Width & Petal.Length & Petal.Width & Species \\ 
        \hline
        1 & 5 & 4 & 1 & 0 & setosa \\ 
        2 & 5 & 3 & 1 & 0 & setosa \\ 
        3 & 5 & 3 & 1 & 0 & setosa \\ 
        4 & 5 & 3 & 2 & 0 & setosa \\ 
        5 & 5 & 4 & 1 & 0 & setosa \\ 
        6 & 5 & 4 & 2 & 0 & setosa \\ 
        \hline
    \end{tabular}
\end{table}
\end{document}

2 Answers2

1

You have not defined R column type. If you want it to do want I guess you mean then you can use wr (with a relatively recent version of the array package) But this is forcing the column widths to values that are inappropriate for the column contents, and forcing the total table width to not fit the page. Just using a natural widths makes the table easier to read, or better remove the vertical lines and use booktabs package.

enter image description here

\documentclass{article}
\usepackage{array,booktabs}
\begin{document}
\begin{table}[ht]
    \centering
    \begin{tabular}{wl{0.015\textwidth}|wr{0.37\textwidth}|wr{0.12\textwidth}|wr{0.08\textwidth}|wr{0.02\textwidth}|wr{0.35\textwidth}|}
        \hline
        & Sepal.Length & Sepal.Width & Petal.Length & Petal.Width & Species \\ 
        \hline
        1 & 5 & 4 & 1 & 0 & setosa \\ 
        2 & 5 & 3 & 1 & 0 & setosa \\ 
        3 & 5 & 3 & 1 & 0 & setosa \\ 
        4 & 5 & 3 & 2 & 0 & setosa \\ 
        5 & 5 & 4 & 1 & 0 & setosa \\ 
        6 & 5 & 4 & 2 & 0 & setosa \\ 
        \hline
    \end{tabular}
\end{table}


\begin{table}[ht]
    \centering
    \begin{tabular}{r|r|r|r|r|l|}
        \hline
        & Sepal.Length & Sepal.Width & Petal.Length & Petal.Width & Species \\ 
        \hline
        1 & 5 & 4 & 1 & 0 & setosa \\ 
        2 & 5 & 3 & 1 & 0 & setosa \\ 
        3 & 5 & 3 & 1 & 0 & setosa \\ 
        4 & 5 & 3 & 2 & 0 & setosa \\ 
        5 & 5 & 4 & 1 & 0 & setosa \\ 
        6 & 5 & 4 & 2 & 0 & setosa \\ 
        \hline
    \end{tabular}
\end{table}


\begin{table}[ht]
    \centering
    \begin{tabular}{cccccl}
        \toprule
        & Sepal.Length & Sepal.Width & Petal.Length & Petal.Width & Species \\ 
        \midrule
        1 & 5 & 4 & 1 & 0 & setosa \\ 
        2 & 5 & 3 & 1 & 0 & setosa \\ 
        3 & 5 & 3 & 1 & 0 & setosa \\ 
        4 & 5 & 3 & 2 & 0 & setosa \\ 
        5 & 5 & 4 & 1 & 0 & setosa \\ 
        6 & 5 & 4 & 2 & 0 & setosa \\ 
        \bottomrule
    \end{tabular}
\end{table}



\end{document}

David Carlisle
  • 757,742
1

You don't need to specify the column widths. Also, you can avoid spreading too much the table by grouping the columns.

\documentclass{article}
\usepackage{booktabs}

\begin{document}

\begin{table}[htp]
\centering

\begin{tabular}{
  @{}
  r  % number
  ccccc
  @{}
}
\toprule
& \multicolumn{2}{c}{Sepal} & \multicolumn{2}{c}{Petal} & Species \\
\cmidrule(lr){2-3} \cmidrule(lr){4-5}
& Length & Width & Length & Width & \\ 
\midrule
1 & 5 & 4 & 1 & 0 & setosa \\ 
2 & 5 & 3 & 1 & 0 & setosa \\ 
3 & 5 & 3 & 1 & 0 & setosa \\ 
4 & 5 & 3 & 2 & 0 & setosa \\ 
5 & 5 & 4 & 1 & 0 & setosa \\ 
6 & 5 & 4 & 2 & 0 & setosa \\ 
\bottomrule
\end{tabular}

\end{table}

\end{document}

enter image description here

egreg
  • 1,121,712