Graphics, Figures & TablesDefine a Table using ctable and tabularx

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
Nuno
Posts: 6
Joined: Thu Jan 13, 2011 1:59 pm

Define a Table using ctable and tabularx

Post by Nuno »

Dear LaTeX users

I need to define a table that imitable the one in figure table.eps. The table must be 50 mm width (column width also controlled according to the drawing), numbers must be aligned by the dot, and the titles must be centered. I'm trying to do it using packages ctable and tabularx. The code I already developed is the following:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\ctable[caption = Long title,
cap = Short title,
width = 50mm,
pos = btp,
label = tab:short]
{r@{}l r@{}l r@{}l}
{>{\hsize=0.5\hsize}X>{\hsize=0.5\hsize}X>{\hsize=2.0\hsize}X}
{
\tnote[\star]{Note 1}
\tnote[\dagger]{Note 2}
\tnote[\ddager]{Note 3}
}
{
\FL
\multicolumn{2}{c}{\textbf{Title 1}} & \multicolumn{2}{c}{\textbf{Title 2}} & \multicolumn{2}{c}{\textbf{Title 3}}\tmark[\star]
\ML
0&.0 & 0&.0 & 0&.0\tmark[\dagger]\NN
0&.0 & 0&.0 & 0&.0\tmark[\ddagger]\LL
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Unfortunately this code contains errors that I'm being unable to overcome. Can anyone assist me on this?

Best regards
Nuno Araújo
Last edited by Nuno on Mon Mar 21, 2011 11:47 am, edited 1 time in total.

Recommended reading 2024:

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

Learn LaTeX easily with newest books:

The LaTeX Beginner's Guide: 2nd edition and perfect for students writing a thesis

The LaTeX Cookbook: 2nd edition full of practical examples for mathematics, physics, chemistry, and more

LaTeX Graphics with TikZ: the first book about TikZ for perfect drawings in your LaTeX thesis

west.logan
Posts: 87
Joined: Tue Sep 14, 2010 6:58 pm

Re: Define a Table using ctable and tabularx

Post by west.logan »

Can you post what class and packages you are using? As in, post a MWE (even though you know it doesn't quite work). Some of your symbols, like the daggers and stars, probably need math mode (put dollar signs "$" around the command).
Nuno
Posts: 6
Joined: Thu Jan 13, 2011 1:59 pm

Define a Table using ctable and tabularx

Post by Nuno »

First of all, my apologies for the late reply. The class and packages I’m using are:

\documentclass[a4paper,twoside,twocolumn,openright]{book}
\usepackage[portuges,UKenglish]{babel}
\usepackage{fontenc}
\usepackage{pstricks}
\usepackage{epsfig}
\usepackage[latin9]{inputenc}
\usepackage{ucs}
\usepackage{makeidx}
\usepackage{hyperref}
\usepackage{fancyhdr}
\usepackage{natbib}
\usepackage{indentfirst}
\usepackage[caption=false]{subfig}
\usepackage{amssymb,amsmath,amsthm}
\usepackage{nextpage}
\usepackage{tocbibind}
\usepackage[Sonny]{fncychap}
\usepackage{epigraph}
\usepackage{ctable}
\usepackage{tabularx}

Regarding the use of \star, \dagger and \ddagger, I clearly forgot to put it in math mode. Thank for the warning.

Regarding the remaining errors, when I remove the code to control column width {>{\hsize=0.5\hsize}X>{\hsize=0.5\hsize}X>{\hsize=2.0\hsize}X} and \multicolumn{2}{c}{\textbf{Title 1,2 and 3}}, the code works perfectly. But, if possible, I would like to use them. Is it possible?

Best regards
Nuno
west.logan
Posts: 87
Joined: Tue Sep 14, 2010 6:58 pm

Re: Define a Table using ctable and tabularx

Post by west.logan »

Thanks for clarifying some things but you still should post a MWE, as per the forum rules.

A MWE is a "minimal working example" which lets others copy and test the code so they don't have to come up with a test document themselves.
Nuno
Posts: 6
Joined: Thu Jan 13, 2011 1:59 pm

Define a Table using ctable and tabularx

Post by Nuno »

I’m new in the forum and I was not aware of that rule. Please find bellow a MWE of my problem.

Regards
Nuno

Code: Select all

\documentclass[a4paper,twoside,twocolumn,openright]{book}

\usepackage{ctable}
\usepackage{tabularx}

\setlength{\columnwidth}{50mm}

\begin{document}

\ctable[caption = Long title,
	         cap = Short title,
	       width = 50mm,
	         pos = btp,
          label = tab:short]		  
{r@{}l r@{}l r@{}l}
{>{\hsize=0.5\hsize}X>{\hsize=0.5\hsize}X>{\hsize=2.0\hsize}X}
{
\tnote[$\star$]{Note 1}
\tnote[$\dagger$]{Note 2}
\tnote[$\ddagger$]{Note 3}
}
{
\FL
\multicolumn{2}{c}{\textbf{Title 1}} & \multicolumn{2}{c}{\textbf{Title 2}} & \multicolumn{2}{c}{\textbf{Title 3}}\tmark[$\star$]
\ML
0&.0 & 0&.0 & 0&.0\tmark[$\dagger$]\NN
0&.0 & 0&.0 & 0&.0\tmark[$\ddagger$]\LL
}	

\end{document}
west.logan
Posts: 87
Joined: Tue Sep 14, 2010 6:58 pm

Define a Table using ctable and tabularx

Post by west.logan »

I'm not able to see your image right now (it seems to have disappeared) but if I remove this line:

Code: Select all

%{>{\hsize=0.5\hsize}X>{\hsize=0.5\hsize}X>{\hsize=2.0\hsize}X}
Then it seems to work and look like what I recalled the image looking like. I left the multicolumn line in. I'm sure there is an easier way to do what you are trying to do though, but I can't see the image right now to be sure.
Nuno
Posts: 6
Joined: Thu Jan 13, 2011 1:59 pm

Re: Define a Table using ctable and tabularx

Post by Nuno »

The look of the table is correct, but I would like to control, if possible, the width of the columns. I can control table width (50 mm) but not the column width. Is there a way to do it?
Attachments
table.png
table.png (26.46 KiB) Viewed 7549 times
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Define a Table using ctable and tabularx

Post by localghost »

Before we continue let me explain why the table setup as presented makes no sense. You're aiming at a width of about (50/6)mm (=8.3…mm) for the first two columns. But the width of their content with the given font size (10pt) is already bigger. See the following example.

Code: Select all

\documentclass[10pt,a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\begin{document}
  \newlength{\testwidth}
  \settowidth{\testwidth}{Title 1}
  Width of ``Title 1'' (in pt): \the\testwidth

  \setlength{\testwidth}{8.33333mm}
  Width of 8.3\ldots mm (in pt): \the\testwidth
\end{document}
Since every length is internally converted to »pt«, I added the width of 8.3…mm in this unit for comparison. As you can see, the demanded column width is not enough for its content. It should be added that you have to subtract 2\tabcolsep from the width for the actual content of a column. So there is still less width.

So you have to clarify whether a decrease of the font size in the table is an option or if the presented content is the one you will have in the actual table. We can do the rest later. However, the example below seems to be at least a partial solution.

Code: Select all

\documentclass[a4paper,twoside,twocolumn,openright]{book}
\usepackage{ctable}
\usepackage{tabularx}

\setlength{\columnwidth}{50mm}

\begin{document}
  \ctable[%
    caption = Long title,
        cap = Short title,
   doinside = \footnotesize,
      width = 50mm,
        pos = btp,
      label = tab:short
  ]       
  {>{\hsize=0.5\hsize}X>{\hsize=0.5\hsize}X>{\hsize=2.0\hsize}X}
  {
    \tnote[$\star$]{Note 1}
    \tnote[$\dagger$]{Note 2}
    \tnote[$\ddagger$]{Note 3}
  }
  {
    \FL
    \multicolumn{1}{c}{\textbf{Title 1}} & \multicolumn{1}{c}{\textbf{Title 2}} & \multicolumn{1}{c}{\textbf{Title 3}}\tmark[$\star$]
    \ML
    0.0  & 0.0  & 0.0\tmark[$\dagger$]\NN
    0.00 & 0.00 & 0.00\tmark[$\ddagger$]\LL
  }   
\end{document}
This is still not satisfactory regarding the table note marks.


Best regards and welcome to the board
Thorsten
Nuno
Posts: 6
Joined: Thu Jan 13, 2011 1:59 pm

Define a Table using ctable and tabularx

Post by Nuno »

Thank Thorsten for your kind welcome

Regarding my column widths, given values are just an example. But what you have point me out explain the reason why a bad box was being reported! In the code bellow I changed the relations between the columns widths to overcome the problem you reported.

What I wish is to control the width of the columns (without changing font size) to manage the look of the tables in my documents. The titles should be center on the column but the numeric data aligned by the dot (with the dot centered on the Title above). I don´t know if it is possible.

Thank you for any comment on this.

Code: Select all

\documentclass[a4paper,twoside,twocolumn,openright]{book}
\usepackage{ctable}
\usepackage{tabularx}

\setlength{\columnwidth}{50mm}

\begin{document}
  \ctable[%
    caption = Long title,
        cap = Short title,
   doinside = \footnotesize,
      width = 50mm,
        pos = btp,
      label = tab:short
  ]       
  {>{\hsize=0.9\hsize}X>{\hsize=0.9\hsize}X>{\hsize=1.2\hsize}X}
  {
    \tnote[$\star$]{Note 1}
    \tnote[$\dagger$]{Note 2}
    \tnote[$\ddagger$]{Note 3}
  }
  {
    \FL
    \multicolumn{1}{c}{\textbf{Title 1}} & \multicolumn{1}{c}{\textbf{Title 2}} & \multicolumn{1}{c}{\textbf{Title 3}}\tmark[$\star$]
    \ML
    1.004  & 0.0  & 0.0\tmark[$\dagger$]\NN
    12.05 & 0.00 & 0.00\tmark[$\ddagger$]\LL
  }   
\end{document}
Nuno
Posts: 6
Joined: Thu Jan 13, 2011 1:59 pm

Define a Table using ctable and tabularx

Post by Nuno »

Dear all

I found the solution to the problem in a document publish by Lapo Filippo Mori, entitle Tables in LATEX2e: Packages and Methods. I just need to use the command \makebox[width][alignment]{text}. Please find below my final solution.

Best regards
Nuno Araújo

Code: Select all

\documentclass[a4paper,twoside,twocolumn,openright]{book}

\begin{document}
	\begin{table}
		\begin{center}
			\caption[Short title]{Long title}
				\begin{tabular}{r@{}l r@{}l r@{}l}
					\\[-1mm]
					\hline\\[-2mm]
					\multicolumn{2}{c}{\makebox[15mm][c]{\textbf{Title 1}}} &
					\multicolumn{2}{c}{\makebox[15mm][c]{\textbf{Title 2}}} &
					\multicolumn{2}{c}{\makebox[20mm][c]{\textbf{Title 3}$^\star$}}\\
					\\[-2mm]
					\hline\\[-2mm]
					\makebox[7.5mm][r]{1}& .12 &
					\makebox[7.5mm][r]{0} & .0 &
					\makebox[10mm][r]{34} & .12$^\dagger$\\
					17&.3 & 123&.1 & 1&.003$^\ddagger$\\
					\\[-2mm]
					\hline\\[-2mm]
					\multicolumn{6}{l}{$^\star$ \footnotesize{Note 1}}\\
					\multicolumn{6}{l}{$^\dagger$ \footnotesize{Note 2}}\\
					\multicolumn{6}{l}{$^\ddagger$ \footnotesize{Note 3}}\\
			\end{tabular}
		\end{center}
	\end{table}
\end{document}
Post Reply