2

I have a simple table with two columns, which should have an equivalent width. I tried to set each column's width to 50% text width. Rightclick -> More -> Settings -> Table Settings -> Width

Now, the columns have an equivalent width,but the table does not respect the text width.

I have another table, where I have set just one column to a width in cm and the other column is not set. In this table everything is fine. The second column adapt automatically, and the table does not overflow the text width.

In both cases, the tables are placed inside a standard float element.

What do I wrong in the first case?

3
  • Can you show code for table? I'm not expert for lyx, but features of tables had to be determined in LaTeX.
    – Zarko
    Sep 20, 2016 at 15:24
  • How much do they extend into the margin? Two columns of 50% texrtwidth will actually make a table slightly wider than textwidth, because there is some space before/between/after columns that is not accounted for. Sep 20, 2016 at 16:09
  • Ok, when I set the width of each column to 47%, it looks fine. But I think this behavior is just plain stupid. It is probably the gap between, the border lines.
    – Johannes
    Sep 20, 2016 at 18:04

1 Answer 1

3

So when LaTeX makes a table it inserts a space of length \tabcolsep at the start and end of each column. The specified width of a column comes in addition to this length. That's why when you have two columns of 0.5\textwidth, the table is a bit wider (4\tabcolsep) than the text width.

There are ways around this, demonstrated in the LaTeX code below. The tabularx package and its X columns can be quite handy, but there is no support for it in the LyX GUI I believe, so you would have to write the table code by hand in an ERT.

To use the second alternative presented, calculating a length, add

p{\dimexpr0.5\linewidth-2\tabcolsep\relax}

in the LaTeX argument box in the table settings in LyX (do it for each column, of course):

enter image description here

This won't let you use the other column settings, so if you want centered text in the cells, use

>{\centering}p{\dimexpr0.5\linewidth-2\tabcolsep\relax}

instead. You also need to go to Document --> Settings --> LaTeX preamble and add

\usepackage{array}

as that package is required for the >{stuff} syntax, but isn't loaded by default. (>{stuff} added before a column specifier, e.g. p{1cm}, inserts stuff at the start of each cell in that column.)

Code example and output

enter image description here

\documentclass{article}
\usepackage{parskip} % just for example, sets zero parindent and some parskip
\usepackage{tabularx}
\begin{document}
What you're doing:

X\dotfill X % illustrates textwidth

\begin{tabular}{| p{0.5\linewidth} | p{0.5\linewidth} |}
\hline
ABC & DEF \\
ABC & DEF \\
\hline
\end{tabular}

X\dotfill X

Removing space between columns (looks bad, don't do it):

X\dotfill X

\begin{tabular}{| @{} p{0.5\linewidth} @{} | @{}p{0.5\linewidth} @{}|}
\hline
ABC & DEF \\
ABC & DEF \\
\hline
\end{tabular}

X\dotfill X

Calculate suitable length:

X\dotfill X 

\begin{tabular}{| p{\dimexpr0.5\linewidth-2\tabcolsep\relax} | p{\dimexpr0.5\linewidth-2\tabcolsep\relax} |}
\hline
ABC & DEF \\
ABC & DEF \\
\hline
\end{tabular}

X\dotfill X

Using \texttt{tabularx} (not available via GUI in LyX):

X\dotfill X

\begin{tabularx}{\linewidth}{| X | X |}
\hline
ABC & DEF \\
ABC & DEF \\
\hline
\end{tabularx}


X\dotfill X


Set the default column sep to zero with \verb|\setlength{\tabcolsep}{0pt}| (I wouldn't do this):
\setlength{\tabcolsep}{0pt}

X\dotfill X


\begin{tabular}{| p{0.5\linewidth} | p{0.5\linewidth} |}
\hline
ABC & DEF \\
ABC & DEF \\
\hline
\end{tabular}

X\dotfill X

\end{document}
2
  • Thank you very much. Your solution works almost perfect, but when I use a Latex argument in Lyx, the column settings grey out, and the text is not centred anymore. Could you help me out with the centred statement?
    – Johannes
    Sep 21, 2016 at 8:26
  • @user59442 >{\centering}p{\dimexpr0.5\linewidth-2\tabcolsep\relax} and add \usepackage{array} in Document -> Settings -> LaTeX preamble. Sep 21, 2016 at 9:00

You must log in to answer this question.

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