1

Problem solved: Package ltablex was loaded before tabularx and thus it didn't work. Using tabularx is working now!

So I would like to set up a table over the whole textwidth with two columns. It's almost working, except the right column is shifted to the right and I don't know how to fix this.

I tried to adapt the solution from this question: Setting table-width exactly to linewidth and this is the code

\documentclass[a4paper, 11pt, oneside]{scrbook}
%\usepackage{tabularx}
\begin{document}
\begin{tabular*}{\linewidth}{@{\extracolsep{\fill}}|l|l|}
    \hline
    left column & right column \\
    \hline
    left column & right column
    \hline
\end{tabular*}
\end{document}

but this is the result:

Latex table with two columns over linewidth with wrong column space

Why is the right column so much shifted to the right? And the left column has zero space to the right. How can I fix this so that the left column uses as much space as needed and the right column fills the linewidth?

My desired output should look like this:

enter image description here

With the left column only using as much space as needed and the right column filling up the rest of the horizontal space.

I already tried it with tabularx but this isn't working either:

\documentclass[a4paper, 11pt, oneside]{scrbook}
\usepackage{tabularx}
\begin{document}
\begin{tabularx}{\linewidth}{|X|X|}
    \hline
    left column & right column \\
    \hline
    left column & right column \\
    \hline
\end{tabularx}
\end{document}

Here is the result:

Not working two column table example with tabularx

Thanks in advance!

Robert
  • 125
  • 1
    Welcome to TeX.SX! Use \noindent before the table. ->\noindent \begin{tabular*}. WIth \usepackage{showframe} you could visualize margins. – Bobyandbob Nov 19 '17 at 21:28
  • Why not use the tabularx package and its flexible width X column? – leandriis Nov 19 '17 at 21:30
  • Thank you! I didn't see the indent but this isn't my main problem. The right column should be left aligned next to the left column without that huge horizontal space in between. – Robert Nov 19 '17 at 21:30
  • Already tried tabularx but with tabularx it isn't working at all. Will update the question with my tabularx example – Robert Nov 19 '17 at 21:33
  • @Robert: could you please turn your code fragment into a compilable MWE? With the information you posted, I am not able to recreate the issue you have with tabularx. – leandriis Nov 19 '17 at 21:37
  • in your table you require, that between columns is maximal available space for \tabcolsep. consequently it is extended to text in second column. what you like actually achieve? equal width of cells? – Zarko Nov 19 '17 at 21:38
  • Ok: Added some code for a MWE and a picture with a desired output (quickly made with a spreadsheet). – Robert Nov 19 '17 at 21:46
  • Wouldn't you be loading \tablex without specifying \keepXColumns? – Bernard Nov 19 '17 at 21:47
  • @Robert - What do you get if you compile the code in my answer (copy-paste as is)? – AboAmmar Nov 19 '17 at 21:52
  • last image in your question is not produced by code which you claim to use for it. see Bernard answer. – Zarko Nov 19 '17 at 22:05

2 Answers2

0

It does work with tabularx:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tabularx}

\begin{document}

\begin{tabularx}{\linewidth}{|X| >{\arraybackslash}X|}
    \hline
    left column & right column \\
    \hline
    left column & right column \\
    \hline
\end{tabularx}

\end{document} 

enter image description here

Bernard
  • 271,350
0

Use this column specification:

\documentclass[a4paper, 11pt, oneside]{scrbook}
\usepackage{tabularx}
\begin{document}
\begin{tabularx}{\linewidth}{|l|X|}
    \hline
    left column & right column \\
    \hline
    left column & right column \\
    \hline
\end{tabularx}
\end{document}

enter image description here

AboAmmar
  • 46,352
  • 4
  • 58
  • 127
  • Problem solved... I had the package ltablex loaded before tabularx and thus it didn't work. It now works and your solution is the best looking! – Robert Nov 19 '17 at 21:56