3

I'm creating table header using LaTeX code mentioned below:

\begin{center}
\begin{tabular}{|c|l|c|c|c|c|}
\hline
$\textbf{Q }$&$\textbf{Header one}$&$\textbf{Test} $&$\textbf{Test1}$&$\textbf{Test2}$&$\textbf{Test3}$\\
$ $&$ $&$\textbf{Text4} $&$\textbf{Text5}$&$\textbf{Text6}$&$\textbf{Text7}$\\
$ $&$ $&$ $&$$&$$&$\textbf{Text8}$\\
\hline
\end{tabular}
\end{center}$

Can you please suggest, how can I arrange this vertically aligned centrally? [Note: I'm looking for option to centrally align text inside header, which is currently spread across three lines.]

3 Answers 3

2

A new verion, after clarification. No additional packages used. Each column is a separate table. $'s left, however rather unneeded.

\documentclass{article} 

\begin{document}

\def\barr{\begin{tabular}{l}}
\def\earr{\end{tabular}}

\begin{tabular}{|c|l|c|c|c|c|}
\hline
$\textbf{Q }$&
\barr 
$\textbf{Header one}$
\earr &
\barr
$\textbf{Test}$\\
$\textbf{Text4} $\\
\earr &
\barr
$\textbf{Test1}$\\
$\textbf{Text5}$
\earr &
\barr
$\textbf{Test2}$\\
$\textbf{Text6}$
\earr &
\barr
$\textbf{Test3}$\\
$\textbf{Text7}$\\
$\textbf{Text8}$
\earr\\
\hline
\end{tabular}


\end{document}

enter image description here

2
  • Sorry, my english is not good; I'm looking for option to centrally align "Text 1 Text5"
    – Pawan Mude
    Nov 22, 2013 at 5:52
  • @PawanMude A new version added. Sorry for misunderstanding. Nov 22, 2013 at 6:49
4

Probably this is what you want:

enter image description here

Code:

\documentclass[12pt,a4paper]{report}
\usepackage{array}
\newcolumntype{M}{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{1.5cm}}

\begin{document}

\begin{center}
\begin{tabular}{|M|m{3cm}|M|M|M|M|}
\hline
\textbf{Q} &
\textbf{Header one} &
\textbf{Test \newline Text4} &
\textbf{Test1 \newline Text5} &
\textbf{Test2 \newline Text6} &
\textbf{Test3 \newline Text7 \newline Text8}\\
\hline
\end{tabular}
\end{center}

\end{document} 

If you want different widths for columns, you can declare the \newcolumntype to have an optional argument, like

\newcolumntype{M}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}

Also, if you want all the table to be in boldface, you can add \bfseries in that declaration

\newcolumntype{M}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}\bfseries}m{#1}}

So, the previous code can be modified as

\documentclass[12pt,a4paper]{report}
\usepackage{array}
\newcolumntype{M}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}\bfseries}m{#1}}
\newcolumntype{N}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}\bfseries}m{#1}}

\begin{document}

\begin{center}
\begin{tabular}{|M{1cm}|N{3cm}|M{1.5cm}|M{1.5cm}|M{1.5cm}|M{1.5cm}|}
\hline
Q &
Header one &
Test \newline Text4 &
Test1 \newline Text5 &
Test2 \newline Text6 &
Test3 \newline Text7 \newline Text8\\
\hline
\end{tabular}
\end{center}

\end{document}

and the result is

enter image description here

4
  • 1
    You could simplify expressions such as \textbf{Test2}\newline\textbf{Text6} to \textbf{Test2 \newline Text6}. :-)
    – Mico
    Nov 22, 2013 at 6:48
  • @karlkoeller ; Thanks for providing solution and updated comment , this is something new I learnt
    – Pawan Mude
    Nov 22, 2013 at 8:15
  • 1
    @Mico Yes, thanks, I was in a hurry... I've modified the code. Nov 22, 2013 at 8:55
  • 1
    @PawanMude You're welcome. I've added some more information. Take a look. Nov 22, 2013 at 8:57
2

If I understand your question correctly, you're looking to create a column type that will wrap long lines while centering the contents of the column.

One method for doing this, which makes use of the array package (a package that's included in all LaTeX distributions I know of), creates a new column type called "C" to do the job. You should, of course, set the width of the column to suit your specific needs.

By the way, there's no need to encase material that's set using \textbf in math mode.

enter image description here

\documentclass{article}
\usepackage{array}
\newcolumntype{C}{>{\centering\arraybackslash}p{1.25cm}}
\begin{document}
\begin{center}
\begin{tabular}{|c|l|C|C|C|C|}
\hline
\textbf{Q} & 
\textbf{Header one} & 
\textbf{Test Text4} & 
\textbf{Test1 Text5} & 
\textbf{Test2 Text6} & 
\textbf{Test3 Text7 Text8}\\
\hline
\end{tabular}
\end{center}
\end{document}
1
  • Thank You for providing comment on above post "\textbf{Test2 \newline Text6}"
    – Pawan Mude
    Nov 22, 2013 at 8:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .