34

I need your help. I want to get a heading text center aligned, but the rest of the table, left aligned only. I tried with some advice in other post: \multicolumn {number of columns}{type of align}{heading} but I don't get it. I want the same result of my solution:

\documentclass[10pt,a4paper]{article}
\title{\textbf{DOC TITLE}}
\usepackage[spanish]{babel}
\usepackage[utf8]{inputenc}

\usepackage{pdflscape}
\usepackage{color, colortbl}
\definecolor{Gray}{gray}{0.9}
\usepackage{enumitem}
\usepackage{anysize}
\marginsize{3cm}{3cm}{2.5cm}{2.5cm}

\usepackage{xcolor}
\usepackage{caption}
\captionsetup[table]{skip=10pt}
\usepackage{threeparttable}
\usepackage{array}

\usepackage{fancyhdr}
\pagestyle{fancy}
\lhead{}
\chead{}
\rhead{\footnotesize{TEXT}}}
\lfoot{}
\cfoot{}
\rfoot{\thepage}
\renewcommand{\headrulewidth}{0.4pt}
\renewcommand{\footrulewidth}{0.4pt}

\begin{document}

\begin{table} [h]
\caption{Title}
\centering
\begin{tabular}{|>{\centering\arraybackslash}m{23}|>{\centering\arraybackslash}m{60}|}
\hline\rowcolor{Gray}
\textbf{text} & \textbf{text}\\ [1ex]
\hline
\begin{flushleft} Text \end{flushleft}\\
\hline
\begin{flushleft} Text \end{flushleft}\\
\hline
text & text
\hline
text & text
\hline
\end{tabular}
\end{table}

\end{document}

I want a table like this, it has errors:Table

Please help me!

  • It is not clear what is your "solution". The code you provided does not compile (missing units in the m specification, missing \\ before some \hlines ...) You didn't provided a MWE, since the preamble is important (apparently packages color and colortbl are required). If your problem is that you don't know how to make your example compile, then you should at least provide a mockup of the desired result. – JLDiaz Dec 19 '12 at 9:15
  • Unrelated to the question but don't just use [h] as that makes it very likely that this table and all following ones to go to the end of the document. always include at least p something like [htp] or not using the option at all would be better. – David Carlisle Dec 19 '12 at 9:29
  • @JLDiaz Thanks for your comment, I edited it. – Isai Dec 19 '12 at 9:41
  • @Isai Still does not compile. And I didn't get yet what is the output you want. Could you provide an image (even a mockup)? – JLDiaz Dec 19 '12 at 9:54
  • @ JLDiaZ I re-edited it. I hope that it is the complete code, because I'm using a phone to do it, and I can't attach an image. – Isai Dec 19 '12 at 10:27
27

It's easier to define the general alignment of the table, and use multicolum to center the header text:

\documentclass[10pt,a4paper]{article}
\usepackage{color, colortbl}
\definecolor{Gray}{gray}{0.9}

\begin{document}
\begin{table}
\caption{Title}
\centering
\begin{tabular}{|>{\raggedright\arraybackslash}m{23mm}|m{23mm}|m{60mm}|}
\hline
\multicolumn{1}{|>{\centering\arraybackslash}m{23mm}|}{\rowcolor{Gray}\textbf{Very long title}} 
    & \multicolumn{1}{>{\centering\arraybackslash}m{23mm}|}{\textbf{Very long title}} 
    & \multicolumn{1}{>{\centering\arraybackslash}m{60mm}|}{\textbf{Normal title}}\\
\hline
Text & Text 
    & Very very very very very very very very very very 
        very veryvery very very very very very very very long text \\
\hline
Very very very very very verryy very very very very very long text 
    & Very very very very very verryy very very very very very long text
    & Text \\
\hline
\end{tabular}
\end{table}
\end{document}

Edit: I have modified my answer to reflect your example.

  • Column 1 is left aligned, as asked, using \raggedright
  • Column 2 is justified (normal paragraphs) with the same content as the column 1
  • Column 3 is justified with a short title

Table

  • Thanks a lot for your answer, but, How can I define the columns width? Because there's an error with: |l{23mm}| – Isai Dec 19 '12 at 9:13
  • @Isai I've changed the definition of the tabular to fit your needs – Lionel MANSUY Dec 19 '12 at 9:19
  • I'm sorry, but the table fits for the heading text, no matter the column width definition. And I've lost the color of the cells heading. – Isai Dec 19 '12 at 9:48
  • @Isai For the color, just put the \rowcolor inside the \multicolumn. For the column width, I do not understand: they are defined to have 23 mm and 60 mm (you should not forget the unit, as pointed above by JLDiaz). – Lionel MANSUY Dec 19 '12 at 10:02
  • 1
    With TeX Live 2018, the \rowcolor{Gray} must come before the \multicolumn{1}, then everything works perfectly. – Kenneth E. Bellock Dec 13 '18 at 22:52
16
\documentclass[10pt,a4paper]{article}
\usepackage[table]{xcolor}
\usepackage{array,ragged2e}

\begin{document}

\begin{table} [!htb]
\caption{Title}
\centering
\begin{tabular}{| m{23pt} | m{60pt}|}\hline
\multicolumn{1}{c}{\cellcolor{black!30}\textbf{text}} & 
  \multicolumn{1}{c}{\cellcolor{black!30}\textbf{text}}\\\hline
Text & Text\\\hline
Text & T \\\hline
text & text\\\hline
text & t\\\hline
\end{tabular}
\end{table}

\end{document}

enter image description here

or for long headings:

\documentclass[10pt,a4paper]{article}
\usepackage[table]{xcolor}
\usepackage{array,ragged2e}

\begin{document}

\begin{table} [!htb]
\caption{Title}
\centering
\begin{tabular}{| m{23pt} | m{60pt}|}\hline
\rowcolor{black!30}\Centering\bfseries foo
  & \Centering\bfseries Her comes some extremely long text for the first heading\\\hline
Text & Text\\\hline
Text & T \\\hline
text & text\\\hline
text & t\\\hline
\end{tabular}
\end{table}

\end{document}

enter image description here

  • the baselines in the lengthy text are really too widely spaced. if a parbox were used with centered text, they'd look more like continuous text. – barbara beeton Dec 19 '12 at 13:43
  • @Herbert Thanks for your answer, but it doesn't work with my table. Maybe I'm wrong: \documentclass[10pt,a4paper]{article} \usepackage[table]{xcolor} \usepackage{array,ragged2e} \begin{document} \begin{table}[h] \caption{Text} \centering \begin{tabular}{|m{23mm}|m{45mm}|m{45mm}|m{45mm}|m{45mm}|} \hline \rowcolor{black!30} \centering\bfseries Text & \centering\bfseries Text & \centering\bfseries Text & \centering\bfseries Text & \centering\bfseries Text\\[0.5ex] \hline Text & Text & Text & Text & Text\\hline \hline \end{tabular} \end{table} \end{document} – Isai Dec 19 '12 at 15:17
  • 1
    @Isai: I used \Centering with an uppercase C, which is important here! – user2478 Dec 19 '12 at 15:39

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.