[Tex/LaTex] tabularx table and the indentation from the addmargin environment

koma-scriptmarginstabularx

I have a problem with the indentation done by the addmargin environment of the koma script and the use of tabularx table. Clearly the \textwidth uses the original width of the text and not the smaller one of the addmargin environment.

So the desired output is that the table is left aligned with the lipsum[1] text and right aligned both with the lipsum[1] and lipsum[2] text.

MWE:

\documentclass{scrbook}
\usepackage{tabularx}
\usepackage{lipsum}


\begin{document}
\lipsum[2]
\begin{addmargin}[2\parindent]{0pt}
    \lipsum[1]  
\begin{tabularx}{\textwidth}{p{.2\textwidth}X}
    First column & A long column which doesn't respect the margin added to the left of this page but goes into the margin on the right \\
    Second column & A long column which doesn't respect the margin added to the left of this page but goes into the margin on the right \\
\end{tabularx}
\end{addmargin}
\end{document}

Best Answer

The \textwidth doesn't change: you have to use \linewidth when inside a list (which addmargin is); moreover you have to remember \noindent and @{} for removing the padding on the left and right.

\documentclass{scrbook}
\usepackage{tabularx}
\usepackage{lipsum}

\begin{document}
\lipsum[2]

\begin{addmargin}[2\parindent]{0pt}
\lipsum[1]

\noindent
\begin{tabularx}{\linewidth}{@{}p{.2\textwidth}X@{}}
    First column & A long column which doesn't respect the margin added to the left of this page but goes into the margin on the right \\
    Second column & A long column which doesn't respect the margin added to the left of this page but goes into the margin on the right \\
\end{tabularx}
\end{addmargin}

\end{document}

enter image description here

On the other hand, tabularx is not needed:

\documentclass{scrbook}
\usepackage{calc}
\usepackage{lipsum}


\begin{document}
\lipsum[2]
\begin{addmargin}[2\parindent]{0pt}
\lipsum[1]  

\noindent
\begin{tabular}{@{}p{.2\linewidth}p{.8\linewidth-2\tabcolsep}@{}}
    First column & A long column which doesn't respect the margin added to the left of this page but goes into the margin on the right \\
    Second column & A long column which doesn't respect the margin added to the left of this page but goes into the margin on the right \\
\end{tabular}
\end{addmargin}

\end{document}
Related Question