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):

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

\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}