LyXProblems with Width of Table Columns

Information and discussion about LyX, a WYSIWYM editor, available for Linux, Windows and Mac OS X systems.
Post Reply
mocham
Posts: 2
Joined: Fri Jan 27, 2012 5:59 am

Problems with Width of Table Columns

Post by mocham »

I'm having a great deal of difficulty figuring out a problem with the way table column widths are dealt with in LyX. The code below shows the problems. I cannot figure out how to make a table with x columns that extend to the text width of the rest of the document. The table "text width %" options do not work as expected as shown in the below code. Any help would be appreciated.

This forum won't let me upload a .lyx file!?!? Anyway, here is the source.

Code: Select all

% Preview source code

%% LyX 2.0.0 created this file.  For more info, see http://www.lyx.org/.
%% Do not edit unless you really know what you are doing.
\documentclass[english]{article}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\usepackage[letterpaper]{geometry}
\geometry{verbose,tmargin=1in,bmargin=1in,lmargin=1in,rmargin=1in}
\setlength{\parskip}{\bigskipamount}
\setlength{\parindent}{0pt}
\usepackage{array}

\makeatletter

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% LyX specific LaTeX commands.
%% Because html converters don't know tabularnewline
\providecommand{\tabularnewline}{\\}

\makeatother

\usepackage{babel}
\begin{document}
This document is setup as a US letter size, with 1'' margins all around.

The table below is set to have a table-wide setting table width of
100\% of the text width. The far ends of the table do line up to 100\%
of the text width, but some other very strange things happen, such
as part of the top border cut off and the text on the first row always
aligned to the right.

\begin{tabular*}{1\textwidth}{@{\extracolsep{\fill}}|c|c|c|}
\cline{2-3} 
\multicolumn{1}{c|}{} & This is a test & test column\tabularnewline
\hline 
123 mph &  & \tabularnewline
\hline 
345 mph &  & \tabularnewline
\hline 
\end{tabular*}

The table below uses no table-wide settings, and columns that are
setup as 33\% of the text width, but for some reason the columns do
not render to 33\% of the text width. You will see when this file
is rendered to PDF, PS, etc...

\begin{tabular}{|>{\centering}p{0.33\textwidth}|>{\centering}p{0.33\textwidth}|>{\centering}p{0.33\textwidth}|}
\cline{2-3} 
\multicolumn{1}{>{\centering}p{0.33\textwidth}|}{} & This is a test & test column\tabularnewline
\hline 
123 mph &  & \tabularnewline
\hline 
345 mph &  & \tabularnewline
\hline 
\end{tabular}

How do I fix this problem?
\end{document}

Recommended reading 2024:

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

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

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Stefan Kottwitz
Site Admin
Posts: 10328
Joined: Mon Mar 10, 2008 9:44 pm

Problems with Width of Table Columns

Post by Stefan Kottwitz »

Hi,
mocham wrote:This forum won't let me upload a .lyx file
I added .lyx to the list of allowed extensions.

Stefan
LaTeX.org admin
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Problems with Width of Table Columns

Post by cgnieder »

Despite the fact that I don't like tables with vertical lines (which in my opion should be avoided unless absolutely necessary) the problem seems to be the \cline which is intended to get lines that span only part of the columns. Its use is not needed here and it can be replaced by \hline.

As for the table to span over \textwidth (or \linewidth which I'd prefer) it is best to use a package which provides a table that does it automatically, such as tabularx or tabu.

Code: Select all

\documentclass[english]{article}
\usepackage[english]{babel}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{siunitx}  % for consistently typeset and nice formatted units
\usepackage{array}
\usepackage{booktabs} % nice looking tables
\usepackage{tabularx} % automatic calculation of column widths
\usepackage{tabu}     % automatic calculation of column widths

\begin{document}

\begin{table}
 \centering
 \caption{Example with \texttt{booktabs} (which I would prefer)}
 \begin{tabular}{lll} \toprule
               & This is a test & test column \\ \midrule
 \SI{123}{mph} &                & \\
 \SI{345}{mph} &                & \\ \bottomrule
 \end{tabular}
\end{table}

\begin{table}
 \caption{Example with \texttt{tabularx}}
 \begin{tabularx}{\linewidth}{|X|X|X|} \hline
               & This is a test & test column \\ \hline
 \SI{123}{mph} &                & \\ \hline
 \SI{345}{mph} &                & \\ \hline
 \end{tabularx}
\end{table}

\begin{table}
 \caption{Example with \texttt{tabu}}
 \begin{tabu} to \linewidth {|X|X|X|} \hline
               & This is a test & test column \\ \hline
 \SI{123}{mph} &                & \\ \hline
 \SI{345}{mph} &                & \\ \hline
 \end{tabu}
\end{table}

\end{document}
site moderator & package author
Post Reply