17

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}
  • 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
32

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}
    
  • 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 6: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 4 '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
4

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}
  • and if I dont want to have the vertical lines bold? – Kostas Dec 16 '15 at 16:27
2

This works with no bold lines.

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

  • save for the probably desired \textbf{}s. – ncmathsadist Jan 30 '17 at 0:07
1

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

0

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: tex.stackexchange.com/a/492145/134574 – Phelype Oleinik Sep 6 at 17:00
  • Thanks for the solution, it also works. – Simon D. Hernandez Sep 10 at 13:31

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.