25

How to create a table using latex - only it's 1st row bold and center and others are left side and normal.

\begin{table}


\renewcommand{\arraystretch}{1.5}


\caption[m1]{ m2}


\label{Hardware}\centering

\begin{tabular}{|c|c|c|c|}

\hline

a & b & c & d  \\ \hline

a  & b  & c & d\\ \hline

a & b & c & d \\ \hline

\end{tabular}

\end{table}
Emalka
  • 1,651
  • 3
  • 22
  • 23
  • 1
    I'm not sure I understand your question: the tabular enviroment in your MWE doesn't seem to include something that looks like a header. (The table's caption is not part of the tabular environment.) In general, you may want to use the command \multicolumn{4}{c}{\bfseries Table header line}} to typeset the header line of a tabular environment that has four columns; "Table header line" would need to be replaced by the actual header you have in mind. Incidentally, you seem to have posted several questions but haven't accepted any of the answers provided; you may want to look into this. – Mico Mar 16 '13 at 16:27
  • I think u could't understand my question...this code will create a table....then i need to bold and center only 1st header row text..I need help for that problem ..thank u – Emalka Mar 17 '13 at 11:59
  • 1
    Have you tried specifying \begin{tabular}{llll} to set the overall table alignment, as well as \multicolumn{1}{c}{\bfseries <Header Col 1>}, \multicolumn{1}{c}{\bfseries <Header Col 2>}, etc. for the entries in the first row? – Mico Mar 17 '13 at 12:18

6 Answers6

42

Tabular does not have a concept of a header. If you want to have one, you have to modify the first row yourself. As Mico pointed out

\begin{tabular}{|l|l|l|l|}
\hline
\multicolumn{1}{c}{\bfseries <Header 1>} & \multicolumn{1}{c}{\bfseries <Header 2>} & \multicolumn{1}{c}{\bfseries <Header 3>} & \multicolumn{1}{c}{\bfseries <Header 4>} \\ \hline
a & b & c & d \\ \hline
a & b & c & d \\ \hline
\end{tabular}

Of course, this is somewhat verbose and cumbersome. Two possible simplifications:

  1. First possibility: Define a macro for this

    \newcommand*{\thead}[1]{\multicolumn{1}{c}{\bfseries #1}}
    

    and then use it:

    \begin{tabular}{|l|l|l|l|}
    \hline
    \thead{<Header 1>} & \thead{<Header 2>} & \thead{<Header 3>} & \thead{<Header 4>} \\ \hline
    a & b & c & d \\ \hline
    a & b & c & d \\ \hline
    \end{tabular}
    
  2. Second possibiliy: Alternatively, use the tabu package, which allows to specify individual row formating via the \rowfont command

    \begin{tabu}{|l|l|l|l|}
    \hline
    \rowfont[c]{\bfseries} <Header 1> & <Header 2> & <Header 3> & <Header 4> \\ \hline
    a & b & c & d \\ \hline
    a & b & c & d \\ \hline
    \end{tabu}
    
Tim Hoffmann
  • 11,159
  • 1
    It worked.thank u..but i need to display all lines of table.In this code header lines are not display.what I need to do? \begin{tabular}{|l|l|} \hline \multicolumn{1}{c}{\bfseries h1} & \multicolumn{1}{c}{\bfseries h2} \ \hline aaaaaaa & bbbbbbb \ \hline eeeeee & ffffff \ \hline \end{tabular} – Emalka Mar 18 '13 at 06:47
  • If I try to deleting only \multicolumn{1}{c}.then lines are drowing but ,how I center only header? – Emalka Mar 18 '13 at 11:11
  • 2
    @Emalka The solution is to do \multicolumn{1}{|c|} – Hunsu May 04 '14 at 13:09
  • @user230137 Your soulution doesn't help, because you double the '|' with every use of \thead. So you'll get the following result: |Header 1||Header 2|| .. ||Header n| – aronadaal Mar 31 '15 at 10:43
  • 1
    Didn't work for me. The vertical lines thing is a fatal flaw. Also, you should use \textbf{}. – ncmathsadist Jan 29 '17 at 23:40
  • Note that as other users stated, in order to use \thead you need to include the package makecell like this: \usepackage{makecell}. – Vassilis Barzokas Feb 16 '20 at 18:40
10

With use of package makecell and its macro \thead it is simple to fulfill requested table format:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{makecell}
\renewcommand\theadfont{\bfseries\sffamily}

\begin{document}
    \begin{tabular}{|l|l|l|l|}
    \hline
\thead{<Header 1>} & \thead{<Header 2>} & \thead{<Header 3>} & \thead{<Header 4>} \\ 
                    \hline
a & b & c & d \\    \hline
a & b & c & d \\    \hline
    \end{tabular}
\end{document}

enter image description here

Zarko
  • 296,517
5

To get the vertical lines in the header of the table properly aligned with the body columns, use the following:

\multicolumn{1}{|c|}{Header 1} & \multicolumn{1}{c|}{Header 2} & \multicolumn{1}{c|}{Header 3}
san
  • 51
3

This works with no bold lines.

\multicolumn{1}{|c|}{\textbf{Header 1}} & \multicolumn{1}{c|}{\textbf{Header 2}} & \multicolumn{1}{c|}{\textbf{Header 3}}

2

Defining a new command like \thead (suggested by Tim Hoffmann) is very useful.

\newcommand*{\thead}[1]{\multicolumn{1}{c}{\bfseries #1}}

However, if you need vertical lines the \multicolumn makes the command useless because you can't set the | manually. I've tried using optional arguments to set the text position but for some reason it didn't worked with tabulary.

I've achieved to center the text without \multicolumn (in tabular, tabulary and longtable) using the following two options.

  1. Using \Centering from the package ragged2e, the problem is that it causes Underfull warnings.
\newcommand*{\thead}[1]{\Centering\bfseries #1}
  1. Using \hspace*{\fill} in both sides
\newcommand*{\thead}[1]{\bfseries\hspace*{\fill}{#1}\hspace*{\fill}}

I'm currently using the second option without issues. I don't have to set the vertical lines and I can use \bfseries.

  • 2
    The optional argument doesn't work because the command becomes non-expandable, but it needs to be. If you load xparse and use \NewExpandableDocumentCommand\thead{O{c}m}{\multicolumn{1}{#1}{\bfseries #2}}, \thead{|c|}{bla} will work :-) See here: https://tex.stackexchange.com/a/492145/134574 – Phelype Oleinik Sep 06 '19 at 17:00
  • Thanks for the solution, it also works. – Simon D. Hernandez Sep 10 '19 at 13:31
2

An alternative solution with tblr environment of tabularray package:

\documentclass{article}

\usepackage{tabularray}

\begin{document}

\begin{tblr}{ colspec = {|l|l|l|l|}, hlines, row{1} = {c,font=\bfseries}, } a & b & c & d \

aa & bb & cc & dd \

aaa & bbb & ccc & ddd \ \end{tblr}

\end{document}

enter image description here

L.J.R.
  • 10,932