2

I am new to LaTex tables and copied a format found online to create a table that would make my column headers over two lines so that the table fitted in when the pdf was Knitted, the code looks like this:

\begin{table}[H]
\centering
\begin{tabular}{|>{\raggedright\arraybackslash}m{15mm}|m{15mm}|m{15mm}|m{15mm}|m{15mm}|} 
\hline
\multicolumn{1}{|>{\centering\arraybackslash}m{15mm}|}{\textbf{Policy Domain}} 
& \multicolumn{1}{>{\centering\arraybackslash}m{15mm}|}{\textbf{Number of Proposed Policy Changes}} 
& \multicolumn{1}{>{\centering\arraybackslash}m{15mm}|}{\textbf{Percent of Proposed Policy Changes}}
& \multicolumn{1}{>{\centering\arraybackslash}m{15mm}|}{\textbf{Total Number of Interest Groups Involved}}
& \multicolumn{1}{>{\centering\arraybackslash}m{15mm}|}{\textbf{Number of Interest Groups per Proposal}}\\
\hline
Economic & 415 & 22.3% & 2574 & 6.2 \\
\hline 
Social Welfare & 414 & 22.2% & 773 & 1.9 \\
\hline
Religious & 166 & 8.9% & 350 & 2.1 \\
\hline
Foreign Policy & 448 & 24.0% & 667 & 1.5 \\
\hline
Firearms & 100 & 5.4% & 99 & 0.9 \\
\hline
Misc. & 320 & 17.2% & 2312 & 7.2 \\
\hline
Total & 1863 & 100% & 6775 & 3.6 \\
\hline
\end{tabular}
\caption{Distribution of proposed policy changes across the main domains}
\label{table:4}
\end{table}

The error I get is this:

! Misplaced \noalign.
\hline ->\noalign 
              {\ifnum 0=`}\fi \let \hskip \vskip \let \vrule \hrule 
\let...
l.444 \hline

Error: Failed to compile Dissertation.tex. See Dissertation.log for more info.
Execution halted

I apologise if this has been asked elsewhere or if I am making a silly error!

  • 2
    \hline needs to be preceded by \\ . You commented the end of the line, because you used % in your cells, which is not a character but the comment symbol in TeX by default. For a percent sign, use \%. But you should not specify the unit in each cell of a column, instead specify it once in the head. – Skillmon May 1 '18 at 11:48
  • Thank you @skillmon (i cant upvote your comment) but it worked – ferris09 May 1 '18 at 12:00
  • 1
    Welcome to TeX.SX! Please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem. – Martin Schröder May 1 '18 at 12:03
3

with use of the package siunitx and defining new column's type C and command \mcx (see mwe below) the code become shorter::

\documentclass{article}
\usepackage[showframe]{geometry}
%\usepackage{float}                       % not used
\usepackage{booktabs, makecell, tabularx}
%\renewcommand{\tabularxcolumn}[1]{m{#1}} % not needed
\renewcommand\theadfont{\normalsize\bfseries}
\renewcommand\theadalign{tc}
\setcellgapes{2pt}
\newcolumntype{C}{>{\centering\arraybackslash}X}
\newcommand\mcx[1]{\multicolumn{1}{>{\bfseries}C|}{#1}}
\usepackage{siunitx}

\begin{document}
    \begin{table}[htb]
\centering\setlength{\tabcolsep}{3pt}
\makegapedcells
\begin{tabularx}{\linewidth}{l|
                              S[table-format=4.0]|
                              S[table-format=2.1,
                                table-space-text-post=\%]<{\,\%}|
                              S[table-format=4.0]|
                              S[table-format=2.1]|
                              }
    \hline
\thead{Policy\\ Domain}
& \mcx{Number of Proposed Policy Changes}
& \mcx{Percent of Proposed Policy Changes}
& \mcx{Total Number of Interest Groups Involved}
& \mcx{Number of Interest Groups per Proposal} \\
    \hline
Economic        &  415 & 22.3   &  2574 & 6.2 \\
    \hline
Social Welfare  &  414 & 22.2   &  773 & 1.9 \\
    \hline
Religious       &  166 & 8.9    &  350 & 2.1 \\
    \hline
Foreign Policy  &  448 & 24.0   &  667 & 1.5 \\
    \hline
Firearms        &  100 & 5.4    &   99 & 0.9 \\
    \hline
Misc.           &  320 & 17.2   & 2312 & 7.2 \\
    \hline
Total           & 1863 & {100}  & 6775 & 3.6 \\
    \hline
\end{tabularx}
\caption{Distribution of proposed policy changes across the main domains}
\label{table:4}
    \end{table}
\end{document}

enter image description here

if you exploit rules defined in the package booktabs and remove all vertical line and the most horizontal, you obtain more "open" and "professionl" looks of table:

enter image description here

\documentclass{article}
\usepackage[showframe]{geometry}
\usepackage{booktabs, makecell, tabularx}
\renewcommand\theadfont{\normalsize\bfseries}
\renewcommand\theadalign{tc}
\setcellgapes{2pt}
\newcolumntype{C}{>{\centering\arraybackslash}X}
\newcommand\mcx[1]{\multicolumn{1}{>{\bfseries}C}{#1}}
\usepackage{siunitx}

\begin{document}
   \begin{table}[htb]
\centering\setlength{\tabcolsep}{3pt}
\makegapedcells
\begin{tabularx}{\linewidth}{@{}
                            l
                            S[table-format=4.0]
                            S[table-format=2.1,
                            table-space-text-post=\%]<{\,\%}
                            S[table-format=4.0]
                            S[table-format=2.1]
                            @{}}
    \toprule
\thead{Policy\\ Domain}
& \mcx{Number of Proposed Policy Changes}
& \mcx{Percent of Proposed Policy Changes}
& \mcx{Total Number of Interest Groups Involved}
& \mcx{Number of Interest Groups per Proposal} \\
    \midrule
Economic        &  415 & 22.3   &  2574 & 6.2 \\
Social Welfare  &  414 & 22.2   &  773 & 1.9 \\
Religious       &  166 & 8.9    &  350 & 2.1 \\
Foreign Policy  &  448 & 24.0   &  667 & 1.5 \\
Firearms        &  100 & 5.4    &   99 & 0.9 \\
Misc.           &  320 & 17.2   & 2312 & 7.2 \\
    \midrule
Total           & 1863 & {100}  & 6775 & 3.6 \\
    \bottomrule
\end{tabularx}
\caption{Distribution of proposed policy changes across the main domains}
\label{table:4}
    \end{table}
\end{document}

addendum:

in case that you like to remove some rows and columns from above table, you only need to delete those rows and contents of column together with ampersands before them. for example, as you ask in comment below:

\documentclass{article}
\usepackage[showframe]{geometry}
\usepackage{booktabs, makecell, tabularx}
\renewcommand\theadfont{\normalsize\bfseries}
\renewcommand\theadalign{tc}
\setcellgapes{2pt}
\newcolumntype{C}{>{\centering\arraybackslash}X}
\newcommand\mcx[1]{\multicolumn{1}{>{\bfseries}C}{#1}}
\usepackage{siunitx}

\begin{document}
   \begin{table}[htb]
%\centering\setlength{\tabcolsep}{3pt}
\makegapedcells
\begin{tabularx}{\linewidth}{@{}
                            l
                            S[table-format=4.0]
                            S[table-format=2.1,
                            table-space-text-post=\%]<{\,\%}
                            S[table-format=4.0]
                            @{}}
    \toprule
\thead{Policy\\ Domain}
& \mcx{Number of Proposed Policy Changes}
& \mcx{Percent of Proposed Policy Changes}
& \mcx{Total Number of Interest Groups Involved}    \\
    \midrule
Economic        &  415 & 22.3   & 2574              \\
Social Welfare  &  414 & 22.2   &  773              \\
Foreign Policy  &  448 & 24.0   &  667              \\
    \midrule
Total           & 1863 & {100}  & 6775              \\
    \bottomrule
\end{tabularx}
\caption{Distribution of proposed policy changes across the main domains}
\label{table:4}
    \end{table}
\end{document}

and you will obtain:

enter image description here

  • If I were to remove a column, how would I go about it? – ferris09 May 1 '18 at 21:23
  • delete this column content (together with one ampersant) as well as column specifier in tabularx. – Zarko May 1 '18 at 21:35
  • I did just that and got: ! LaTeX Error: Command \mcx already defined. Or name \end... illegal, see p.192 of the manual. – ferris09 May 1 '18 at 21:41
  • you obviously did something different than I suggested :-). it's probably a misunderstanding. tell me which column you would like to delete, and I will show how to do this in the addition to my answer. – Zarko May 1 '18 at 21:49
  • Youre too kind - I am trying to remove the last column, I removed S[table-format=2.1] and the last columns figures and ampersands that went with it and then removed the & \mcx{Number of Interest Groups per Proposal}, additionally I deleted the rows Misc., Firearms, and religious – ferris09 May 1 '18 at 21:51
1

You were missing some \arraybackslash. Anyway your columns were not wide enough. I propose to load geometry, to have sensible margins, tabularx and some other candies:

    \documentclass{article}
    \usepackage[showframe]{geometry}
    \usepackage{tabularx, booktabs, makecell, float}
    \renewcommand{\tabularxcolumn}[1]{m{#1}}
    \renewcommand\theadfont{\normalsize\bfseries}
    \renewcommand\theadalign{tc}
    \begin{document}

    \begin{table}[H]
    \centering\setlength{\tabcolsep}{3pt}
    %\setlength{\extrarowheight}{2pt}
    \renewcommand{\arraystretch}{1.5}
    \begin{tabularx}{\linewidth}{|l|*{4}{>{\centering\arraybackslash}X|}}%
    \hline
    \thead{Policy\\ Domain}
    & \multicolumn{1}{>{\centering\arraybackslash}X|}{\textbf{Number of Proposed Policy Changes}}
    & \multicolumn{1}{>{\centering\arraybackslash}X|}{\textbf{Percent of Proposed Policy Changes}}
    & \multicolumn{1}{>{\centering\arraybackslash}X|}{\textbf{Total Number of Interest Groups Involved}}
    & \multicolumn{1}{>{\centering\arraybackslash}X|}{\textbf{Number of Interest Groups per Proposal}}\\
    \hline
    Economic & 415 & 22.3\,\% & 2574 & 6.2 \\
    \hline
    Social Welfare & 414 & 22.2\,\% & 773 & 1.9 \\
    \hline
    Religious & 166 & 8.9\,\% & 350 & 2.1 \\
    \hline
    Foreign Policy & 448 & 24.0\,\% & 667 & 1.5 \\
    \hline
    Firearms & 100 & 5.4\,\% & 99 & 0.9 \\
    \hline
    Misc. & 320 & 17.2\,\% & 2312 & 7.2 \\
    \hline
    Total & 1863 & 100\,\% & 6775 & 3.6 \\
    \hline
    \end{tabularx}
    \caption{Distribution of proposed policy changes across the main domains}
    \label{table:4}
    \end{table}

    \end{document} 

enter image description here

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.