Graphics, Figures & TablesTable using /multirow

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
majordanby
Posts: 6
Joined: Wed Aug 25, 2010 6:47 pm

Table using /multirow

Post by majordanby »

Hi guys,

I feel like I have to apologise for my first post here being a question, but I have been searching for an answer for a long time and have not yet been able to find one to my satisfaction. Hopefully you can help me :)


I am trying to create a table using multirow, and am trying to make it in keeping with the rest of my document.

My effort so far is as follows:
Image

with the code:

Code: Select all

\begin{table}
\centering
\begin{tabular}{| l | c c c | c c c|}

&  \multicolumn{3}{ |c| }{Prior LTP} &  \multicolumn{3}{ c| }{Post LTP} \\ \hline \hline
Frequency & Capacitance & Loss & $\epsilon_r$ & Capacitance & Loss & $\epsilon_r$\\ \hline
0.1 & c&l&p&c&l&p\\
1 & c&l&p&c&l&p\\
10 & c&l&p&c&l&p\\
100 & c&l&p&c&l&p\\
\hline
\label{tab:stackperm}
\end{tabular}
\end{table}
My problem with the above example, is that I would like the left bounding line to start at the double line above 'Frequency' and not at the top of the table. Also the bounding lines on the left of the table finish below the table for some reason that I can't fathom.

Incidentally, this is my second attempt at the formatting of this table. For it to fit in exactly with the rest of my document, it should be formatted more along the lines of:
Image

with the code:

Code: Select all

\begin{table}
\centering
\begin{tabular}{l | c c c | c c c}\hline\hline
&  \multicolumn{3}{ |c| }{Prior LTP} &  \multicolumn{3}{ c }{Post LTP} \\ \hline \hline
Frequency & Capacitance & Loss & $\epsilon_r$ & Capacitance & Loss & $\epsilon_r$\\ \hline
0.1 & c&l&p&c&l&p\\
1 & c&l&p&c&l&p\\
10 & c&l&p&c&l&p\\
100 & c&l&p&c&l&p\\
\hline
\label{tab:stackperm}
\end{tabular}
\end{table}
However, here I still have the trouble with the left hand bounding line finishing below the table, and ideally, if I were to use this format, I would like the top double \hline to not start at the far left of the table, but instead start after the empty cell in the top left. Not sure if this is possible or not, but any help would be greatly appreciated. I must admit I am starting to pull my hair out!!

Cheers all

P.S. please ignore the fact that I haven't entered any useful data into my table as of yet! :D

Thanks again
Last edited by majordanby on Wed Aug 25, 2010 8:20 pm, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

frabjous
Posts: 2064
Joined: Fri Mar 06, 2009 12:20 am

Table using /multirow

Post by frabjous »

Is this the kind of thing you want:

Code: Select all

\documentclass{article}
\begin{document}
    \begin{table}
    \centering
    \begin{tabular}{| l | c c c | c c c|}
    \hline\hline
    \multicolumn{1}{l|}{} &  \multicolumn{3}{ |c| }{Prior LTP} &  \multicolumn{3}{ c }{Post LTP} \\ \hline \hline
    Frequency & Capacitance & Loss & $\epsilon_r$ & Capacitance & Loss & $\epsilon_r$\\ \hline
    0.1 & c&l&p&c&l&p\\
    1 & c&l&p&c&l&p\\
    10 & c&l&p&c&l&p\\
    100 & c&l&p&c&l&p\\ 
    \hline
    \end{tabular}
    \label{tab:stackperm}
    \end{table}
\end{document} 
table.png
table.png (6.37 KiB) Viewed 9108 times
The spurious extending lines on the left were from your label being inside the tabular environment rather than just inside the table environment. It thought label needed to be inside a cell, so it created one for it.
majordanby
Posts: 6
Joined: Wed Aug 25, 2010 6:47 pm

Re: Table using /multirow

Post by majordanby »

Thanks for the prompt reply frabjous. I take it that adding the first multicolumn command spanning just 1 column stops the left bounding line from stretching to the top of the column?

Sorry I should have realised that I had added my label was in the wrong place!

That has almost exactly answered my query. I have just one more question, is there anyway to stop the top double \hline from spanning the entire table? Ideally I would like it to just span the two columns labelled Pre LTP and Post LTP (leaving the top left empty cell free of any borders).

This is not such a big problem if not, but for the sake of completeness it would be nice to know if it was possible for the future.

Thanks again.
User avatar
frabjous
Posts: 2064
Joined: Fri Mar 06, 2009 12:20 am

Table using /multirow

Post by frabjous »

majordanby wrote:Thanks for the prompt reply frabjous. I take it that adding the first multicolumn command spanning just 1 column stops the left bounding line from stretching to the top of the column?
It just allowed me to put in whatever I wanted for that cell in the second argument. I put in {l|}, which means left justified with a line only on the left. {|l|} would have kept both lines, or {l} would have removed both, and so on.

There might be a more elegant way to do that, but without loading packages, I don't know what they are offhand.
That has almost exactly answered my query. I have just one more question, is there anyway to stop the top double \hline from spanning the entire table? Ideally I would like it to just span the two columns labelled Pre LTP and Post LTP (leaving the top left empty cell free of any borders).
Change \hline\hline to \cline{2-7}\cline{2-7}. (Strictly speaking, there are seven columns in your table; \cline{2-7} makes it draw the horizontal line just from columns 2-7.)

You'll probably also want to change \multicolumn{3}{ |c| }{Prior LTP} to just \multicolumn{3}{c|}{Prior LTP} to remove the left border on that cell. (Or else add one back in on the other side.)
majordanby
Posts: 6
Joined: Wed Aug 25, 2010 6:47 pm

Re: Table using /multirow

Post by majordanby »

Brilliant mate. Exactly what I wanted.

Many thanks, and have a good evening.
majordanby
Posts: 6
Joined: Wed Aug 25, 2010 6:47 pm

Re: Table using /multirow

Post by majordanby »

Actually frab,

\cline{2-7}\cline{2-7} just seems to superimpose the two lines on top of one another (although it does do it from columns 2-7 of course. Any ideas??

Sorry to keep bringing this up :D
majordanby
Posts: 6
Joined: Wed Aug 25, 2010 6:47 pm

Table using /multirow

Post by majordanby »

Well, after much searching I have found a very inelegant solution.

The \cline{}\cline{} commands do superimpose the two lines on top of one another. The only way I have found to get around this is to use the \morecmidrules command.

Code: Select all

\cmidrule{2-7} \morecmidrules \cline{2-7}
The \morecmidrules command only works when preceded by the \cmidrule command, i.e.

Code: Select all

\cline{} \morecmidrules \cline{}
does not work. It needs to contain the \cmidrule command first.

I hope there is a more elegant way to accomplish this, but I can't find it if there is.
Image

with the code:

Code: Select all

\begin{table}
\centering
\begin{tabular}{l|ccc|ccc}\cmidrule{2-7} \morecmidrules \cline{2-7}
&  \multicolumn{3}{ |c| }{Prior LTP} &  \multicolumn{3}{ c }{Post LTP} \\ \hline \hline
Frequency (kHz) & Capacitance (F) & Loss & $\epsilon_r$ & Capacitance (F) & Loss & $\epsilon_r$\\ \hline
0.1 & c&l&p&c&l&p\\
1 & c&l&p&c&l&p\\
10 & c&l&p&c&l&p\\
100 & c&l&p&c&l&p\\
\hline
\end{tabular}
\label{tab:stackperm}

\end{table}
The only problem is that the distance between the two lines written with code, \cmidrule{2-7} \morecmidrules \cline{2-7}, is that they are slightly further apart that when using \hline\hline

Please note, if you use the code

Code: Select all

\cmidrule{2-7} \morecmidrules \cmidrule{2-7}
you will get a gap between the bottom line and the rest of the table, like so:
Image
User avatar
frabjous
Posts: 2064
Joined: Fri Mar 06, 2009 12:20 am

Table using /multirow

Post by frabjous »

Sorry I should have tested the double \cline.

Probably the easiest thing if you don't mind it would be to load the hhline package which will give you complete control over these things, e.g.:

\hhline{~======}

Will produce a double line from rows 2-7. You can even control how the double and single lines intersect; e.g., use:

\hhline{~|======|}

or

\hhline{~|===|===|}

to make the vertical lines go up to the top of the first of the double lines.

You'll probably also want to change the double \hline that comes later to:

\hhline{=======}

or

\hhline{|=|===|===|}

so that they match.

You can do a lot more too, see its documentation. I was just trying to keep it simple before.
majordanby
Posts: 6
Joined: Wed Aug 25, 2010 6:47 pm

Table using /multirow

Post by majordanby »

Excellent frabjous.

No worries about the \cline command :D

Using \hhline I now have a lovely looking table:
Image


Code: Select all

\begin{table}
\centering
\begin{tabular}{l|ccc|ccc}\hhline{~======}
&  \multicolumn{3}{ c| }{Prior LTP} &  \multicolumn{3}{ c }{Post LTP} \\ \hhline{=|===|===}
Frequency (kHz) & Capacitance (F) & Loss & $\epsilon_r$ & Capacitance (F) & Loss & $\epsilon_r$\\ \hline
0.1 & c&l&p&c&l&p\\
1 & c&l&p&c&l&p\\
10 & c&l&p&c&l&p\\
100 & c&l&p&c&l&p\\
\hhline{=======}
\end{tabular}
\label{tab:stackperm}

\end{table}

Thanks for all your help, you've been brilliant.

Cheers
Post Reply