1

I have to make the table in the figure, but I get several errors. Could you explain what are my mistakes, please? (I did not put the numbers in my code...I want to just understand how this works)

enter image description here

\begin{document}
\begin{tabularx} {\textwidth} {cccccc}
\bottomrule
Regione & Assistiti & di cui over 65 & di cui under 65 & % anziani & % under 65
\midrule
Italia & x & x & x & x& x //
Nord & x & x & x & x& x //
Centro & x & x & x & x& x //
Sud & x & x & x & x& x //
Basilicata & x & x & x & x& x //
\bottomrule
end{tabularx}
\end{document}
  • 6
    You should not use // instead of \\ . – user121799 Feb 13 '18 at 13:12
  • 6
    and you should write \% instead of % otherwise LaTeX considers the following words as comments – CarLaTeX Feb 13 '18 at 13:14
  • 3
    also the options on the align are {lrrrrr} because you want first column left aligned and tho others right and not centered. Or you have to look at siunitx package to align on "." the numbers – koleygr Feb 13 '18 at 13:15
  • 4
    and don't use tabularx if you don't have an X column type – CarLaTeX Feb 13 '18 at 13:15
  • 2
    Also, at the top of a table, preferably (for spacing reasons) put a \toprule. – Bernard Feb 13 '18 at 13:16
8

As we said in comments:

  1. you should use \\, not // at the and of the table rows
  2. % is a special char for LaTeX, you should write \% if you use it as a percentage, otherwise LaTeX considers what follows % as a comment
  3. at the top of a table, for spacing reasons, you should use \toprule, not \bottomrule (that's why they have named them in that way)
  4. you should not use tabularx if you don't have an X column type.

To explain better the last point, look at these examples:

\documentclass{article}
\usepackage[utf8]{inputenc}

\usepackage{booktabs}
\usepackage{tabularx}
\begin{document}
    In the following example, since the table is longer than the line width
    (we have an \verb|Overfull \hbox| error), the lines stop before the text
    ends:

    \begin{center}
    \begin{tabularx}{\linewidth}{cc}
        \toprule
        Don't try this at home, this is wrong! & Non fatelo, è sbagliato!\\
        \midrule
        Example of wrong use of \texttt{tabularx} & Esempio di un uso sbagliato di \texttt{tabularx}\\
        \bottomrule
    \end{tabularx}
    \end{center}

    In the following example, since the table is too much shorter than the
    line width, the lines go too much ahead w.r.t. the text:

    \begin{center}
    \begin{tabularx}{\linewidth}{cc}
        \toprule
        This is wrong! & Questo è sbagliato!\\
        \midrule
        Wrong use & Uso sbagliato\\
        \bottomrule
    \end{tabularx}
    \end{center}

    Whereas, with \verb|X| columns:
    \begin{center}
    \begin{tabularx}{\linewidth}{XX}
        \toprule
        Try this at home, this is correct! & Questo si può fare, è giusto!\\
        \midrule
        Example of correct use of \texttt{tabularx} & Esempio di un uso corretto di \texttt{tabularx}\\
        \bottomrule
    \end{tabularx}
    \end{center} 
    \begin{center}
    \begin{tabularx}{\linewidth}{XX}
        \toprule
        This is correct! & Questo è corretto!\\
        \midrule
        Correct use & Uso corretto\\
        \bottomrule
    \end{tabularx}
    \end{center}
\end{document}

enter image description here

Applying all that, here is a working code (since your table is lager than the line width I added \footnotesize to make the font smaller).

For the alignment, please note that l means left, r right, and c center.

\documentclass{article}
\usepackage{booktabs}

\begin{document}
\begin{center}
\footnotesize\begin{tabular}{lrrrrr}
\toprule
Regione & Assistiti & di cui over 65 & di cui under 65 & \% anziani & \% under 65\\
\midrule
Italia & x & x & x & x& x \\
Nord & x & x & x & x& x \\
Centro & x & x & x & x& x \\
Sud & x & x & x & x& x \\
Basilicata & x & x & x & x& x \\
\bottomrule
\end{tabular}
\end{center}
\end{document}

enter image description here

Of course there are other solutions, here is an example with tabularx. Since the X column is ragged right by default, I created a new column type R to have the columns ragged left.

*5R is a shorcut for RRRRR.

\documentclass{article}
\usepackage{array}
\usepackage{tabularx}
\usepackage{booktabs}
\newcolumntype{R}{>{\raggedleft\arraybackslash}X}
\begin{document}
\begin{tabularx}{\linewidth}{l*5R}
\toprule
Regione & Assistiti & di cui over 65 & di cui under 65 & \% anziani & \% under 65\\
\midrule
Italia & x & x & x & x& x \\
Nord & x & x & x & x& x \\
Centro & x & x & x & x& x \\
Sud & x & x & x & x& x \\
Basilicata & x & x & x & x& x \\
\bottomrule
\end{tabularx}
\end{document}

enter image description here

  • Thank you! May I know why do you use (1) \begin{center} (2) {lrrrrr} – Always learning Feb 13 '18 at 13:25
  • @Alwayslearning The center environment centers your table. You can also use table environment if you have a caption. – CarLaTeX Feb 13 '18 at 13:26
  • 2
    @Alwayslearning, the {lrrrrr} corresponds to the outline of the columns within the table. You have 6 columns and they are outlined "left", "right", "right", "right", "right", "right", just like in your example. Change one of these letters to "c" (stands for center) and see what happens! – JiyuuSensei Feb 13 '18 at 13:32
  • @Alwayslearning See also Zarko's answer, he added the treatment of columns with numbers. – CarLaTeX Feb 13 '18 at 13:43
  • 1
    @Alwayslearning instead of using \\, leave a blank line. – CarLaTeX Feb 13 '18 at 18:05
5

considering all comments below question ... and add some my suggestions:

\documentclass{article}
\usepackage{adjustbox}

%-------------------------------- show page layout, only for test
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%
\usepackage{booktabs}
\usepackage{siunitx}

\begin{document}
    \begin{table}[htb]
\sisetup{group-minimum-digits=4,
         group-separator={.},
         output-decimal-marker={,}}
\setlength\tabcolsep{0pt}
\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}}
                             l
                        *{2}{S[table-format=6.0]}
                             S[table-format=5.0]
                        *{2}{S[table-format=2.2]}
                             }
 \toprule
Regione & {Assistiti} & {di cui over 65} & {di cui under 65} & {\% anziani} & {\% under 65} \\
    \midrule
Italia  & 123456      & 12345            & 1234              & 12,34        & 12,34         \\
Nord    & 12345       & 123456           & 123               & 12,34        & 12,34         \\
Centro  & 123456      & 12345            & 12345             & 12,34        & 12,34         \\
Sud     & 123456      & 12345            & 1234              & 12,34        & 12,34         \\
    \bottomrule
\end{tabular*}
    \end{table}
\end{document}

enter image description here

added red lines are only for show page layout.

  • in the first shot i load wrong version of code (with missing end :-( ) and wrong image. now this is corrected. is now table still not acceptable to you? if it, please show us, how you will do :-) – Zarko Feb 13 '18 at 13:51
  • @CarLaTeX... I show it and deleted my comment... In Greek too (:P) – koleygr Feb 13 '18 at 13:51
  • It is fine! Sorry for hurrying to comment before really understand (my problem is what CarLaTeX understood) – koleygr Feb 13 '18 at 13:55
  • @koleygr, thank you for pointing to problem of my code. fortunately i observe this problem almost in the same time as you ... and try to put my cyber chaos in some order :-). i understood your comment very positive and in my response try to challenge/encourage you, to write (better) solution :-) – Zarko Feb 13 '18 at 14:21
0

Try this

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tabularx}

\begin{document}
\begin{tabularx} {\textwidth} {cccccc}
\hline
Regione & Assistiti & di cui over 65 & di cui under 65 & \% anziani & \% under 65 \\
\hline
Italia      & x & x & x & x & x \\
Nord        & x & x & x & x & x \\
Centro      & x & x & x & x & x \\
Sud         & x & x & x & x & x \\
Basilicata  & x & x & x & x & x \\
\hline
\end{tabularx}
\end{document}
  • it does not do the job well – Always learning Feb 13 '18 at 13:37
  • 1
    It works...I forgot to add the package before. Thanks! – Always learning Feb 13 '18 at 13:40
  • 1
    1) As already said, using tabularx without an X column is useless. 2) Due to 1), the last columns header goes outside the table because the table itself is larger than the text width. – CarLaTeX Feb 13 '18 at 14:05

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.