2

I want to shift my table slightly to the left of the page (exceeding left text width boundary). After many googles I however keep having trouble and can't manage to get ˙\adjustwidth` to work:

\documentclass[a4paper, 12pt]{article}

\usepackage[margin=1.1in]{geometry}
\usepackage{tcolorbox}
\usepackage{color}
\usepackage{listings}
\usepackage{titlesec}
\usepackage{ragged2e}
\usepackage{fancyhdr}
\usepackage{graphicx}
\usepackage{booktabs, ltablex, makecell}
\usepackage{siunitx}[input-decimal-markers=.]
\usepackage{lipsum}
\usepackage{threeparttable}
\usepackage{pdflscape}
\usepackage[strict]{changepage}
\usepackage{enumitem}
\usepackage{makecell}
\usepackage{array}
\usepackage[sort, numbers, compress]{natbib}
\usepackage{amsmath}
\usepackage{mathabx}
\usepackage{makebox}
\usepackage[font=footnotesize,labelfont=bf,labelsep=space ]{caption}
\usepackage[title]{appendix}
\usepackage[export]{adjustbox}
\usepackage[hidelinks]{hyperref}
\renewcommand\leq\varleq
\renewcommand\geq\vargeq
\interfootnotelinepenalty=10000
\addtolength{\skip\footins}{10pt}
\captionsetup[table]{singlelinecheck=false,justification=raggedright}

%
%\newcolumntype{L}{>{\raggedright\arraybackslash}X}
%\newcolumntype{b}{>{\hsize=0.75\hsize}X}
%\newcolumntype{B}{>{\hsize=1.25\hsize \raggedright\arraybackslash}X}
\renewcommand\theadfont{\footnotesize\bfseries}
\renewcommand\theadgape{}



\usepackage{showframe}


\begin{document}

\begin{adjustwidth}{-1in}{-1cm}
\footnotesize
%   \renewcommand{\theadalign}{lbc}
\renewcommand{\arraystretch}{1.4}
\begin{tabularx}{\linewidth}{lXXXXX}
        \caption{caption}\label{resultstable}\\
        \toprule
        \thead[l]{\footnotesize  \vspace{-6mm} variables} & \thead{\footnotesize  \\ 1 \\ } & \thead{\footnotesize Cluster \\ 2 \\(n=111)} & \thead{\footnotesize  \\ 3 \\(n=111)} &
        \thead{\footnotesize  \\4\\()} & \thead{\footnotesize  \\5\\()} \\
        \midrule
        \endfirsthead 

        \caption{Continued.}\\
        \toprule
        \thead[l]{\footnotesize \vspace{-6mm} 1} & \thead{\footnotesize  1 \\ } & \thead{\footnotesize  2} & \thead{\footnotesize  3} &
        \thead{\footnotesize  4} & \thead{\footnotesize 5} \\
        \midrule
        \endhead


        \textit{Very very very very Long variable name}&\\ 
        \hspace{1mm}var2\\
        \qquad varx& 0.92 (0.13)& 0.65 (0.26) & 0.65 (0.19)& 0.49 (0.23)& 
        0.44 (0.23)\\
        \qquad vary& 0.93 (0.12)& 0.52 (0.28) & 0.53 (0.21) & 0.23 (0.22)&0.02 (0.07)\\
        \qquad varz&\\
        \qquad varu&\\
        \qquad varrn&\\ 
        \hspace{1mm}varrqe&\\
        \qquad var&\\
        \qquad var&\\
        \qquad var&\\
        \qquad var&\\
        \qquad var&\\ 
        \hspace{1mm}var)&\\


        \bottomrule

\end{tabularx}
\end{adjustwidth}

\end{document} 

It perfectly extends the table to the right, but will not do so for the left side. It does however extend the caption to the left, but not the whole table? Does anybody know why this is? Is it perhaps because of the multitude of used packages?

1 Answer 1

2

you have many issues with your table:

  • ltablex doesn't work well with \adjustwidth. is your table really multi page long?
  • use of ltablex preserve X column tape only if cells' contents are wider than standard width of X column, otherwise it shrink column width to width if you have l column tape. a cure is add \keepXColumns to document preamble
  • regardless that you have contents only one column of table (and others empty), you need always to write all ampersands. adding missed ampersands fixed your mine problem (if you not use ltablex but simple tabularx).
  • \adjustwidth is not intended to change text width of more pages. for this is more appropriate to use \newgeometry from geometry package

consider above comments, the table can be written as:

\documentclass[a4paper, 12pt]{article}
\usepackage[margin=1.1in]{geometry}
\usepackage{booktabs,
            makecell,
            tabularx,
            threeparttable}
\renewcommand\theadfont{\footnotesize\bfseries}
\renewcommand\theadgape{}
\newcommand\mcl[1]{\multicolumn{1}{l}{#1}}
\usepackage[strict]{changepage}
\usepackage[font=footnotesize,
            labelfont=bf,
            labelsep=space]{caption}
\captionsetup[table]{singlelinecheck=false,
                     justification=raggedright}

%---------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%

\begin{document}
\begin{table}[htb]
    \begin{adjustwidth}{-1cm}{-1cm}
    \footnotesize
    \renewcommand{\arraystretch}{1.4}
\caption{caption}
    \label{resultstable}
\begin{tabularx}{\linewidth}{>{\quad}l X X X X X }
    \toprule
\thead[b]{variables}
    & \thead[b]{1}
        & \thead[b]{Cluster \\ 2 \\(n=111)}
            & \thead[b]{3 \\(n=111)}
                & \thead[b]{4\\()}
                    & \thead[b]{5\\()}      \\
    \midrule
\mcl{\textit{Very very very very Long variable name}}
            &   &   &   &                   \\
\mcl{var2}  &   &   &   &   &               \\
      varx  & 0.92 (0.13)
                & 0.65 (0.26)
                    & 0.65 (0.19)
                        & 0.49 (0.23)
                            & 0.44 (0.23)   \\
      vary  & 0.93 (0.12)
                & 0.52 (0.28)
                    & 0.53 (0.21)
                        & 0.23 (0.22)
                            & 0.02 (0.07)   \\
\mcl{varz}  &   &   &   &   &               \\
      varu  &   &   &   &   &               \\
      varrn &   &   &   &   &               \\
     varrqe &   &   &   &   &               \\
\mcl{var)}  &   &   &   &   &               \\
    \bottomrule
\end{tabularx}
\end{adjustwidth}
    \end{table}
\end{document}

which gives:

enter image description here

if you make Very very very very Long variable name shorter, for example with use of abrevation or with break it into two or more lines, than the use of \adjustwidth wouldn't be necessary anymore. in this case use of ltablex for this table will works as expected:

\documentclass[a4paper, 12pt]{article}

\usepackage[margin=1.1in]{geometry}

\usepackage{booktabs,
            longtable,
            ltablex,
            makecell,
            threeparttable}
\keepXColumns
\renewcommand\theadfont{\footnotesize\bfseries}
\renewcommand\theadgape{}
\newcolumntype{C}{>{\centering\arraybackslash}X}
\newcolumntype{L}{>{\raggedright\arraybackslash}X}
\newcommand\mcx[1]{\multicolumn{1}{@{}>{\hsize=1.75\hsize}X}{#1}}
\usepackage[strict]{changepage}
\usepackage[font=footnotesize,
            labelfont=bf,
            labelsep=space]{caption}
\captionsetup[table]{singlelinecheck=false,
                     justification=raggedright}

%---------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%

\begin{document}
    \footnotesize
    \renewcommand{\arraystretch}{1.4}
\begin{tabularx}{\linewidth}{@{}>{\quad\hsize=1.75\hsize}L 
                                 *{5}{>{\hsize=0.95\hsize}C}
                             @{}}
\caption{caption}
    \label{resultstable}                    \\
    \toprule
\thead[b]{variables}
    & \thead[b]{1}
        & \thead[b]{Cluster \\ 2 \\(n=111)}
            & \thead[b]{3 \\(n=111)}
                & \thead[b]{4\\()}
                    & \thead[b]{5\\()}      \\
    \midrule
\endfirsthead
\caption{caption (cont.)}
    \label{resultstable}                    \\
    \toprule
\thead[b]{variables}
    & \thead[b]{1}
        & \thead[b]{Cluster \\ 2 \\(n=111)}
            & \thead[b]{3 \\(n=111)}
                & \thead[b]{4\\()}
                    & \thead[b]{5\\()}      \\
    \midrule
\endhead
    \midrule
\multicolumn{6}{r}{\textit{continue on the next page}}
\endfoot
    \bottomrule
\endlastfoot
\mcx{\itshape
     Very very very very Long variable name}
            &   &   &   &                   \\
\mcx{var2}  &   &   &   &   &               \\
      varx  & 0.92 (0.13)
                & 0.65 (0.26)
                    & 0.65 (0.19)
                        & 0.49 (0.23)
                            & 0.44 (0.23)   \\
      vary  & 0.93 (0.12)
                & 0.52 (0.28)
                    & 0.53 (0.21)
                        & 0.23 (0.22)
                            & 0.02 (0.07)   \\
\mcx{varz}  &   &   &   &   &               \\
      varu  &   &   &   &   &               \\
      varrn &   &   &   &   &               \\
     varrqe &   &   &   &   &               \\
\mcx{var)}  &   &   &   &   &               \\
\end{tabularx}
\end{document}

enter image description here

addendum: According to your comment below, (one of the possible) solutions is to add a one column to the table and to redesign the \mcx command. i also suggest to ad more vertical space before row with \mcx command (all changes are in code indicated by % <---):

\documentclass[a4paper, 12pt]{article}
\usepackage[margin=1.1in]{geometry}

\usepackage{booktabs,
            longtable,
            ltablex,
            makecell,
            threeparttable}
\keepXColumns
\renewcommand\theadfont{\footnotesize\bfseries}
\renewcommand\theadgape{}
\newcolumntype{C}{>{\centering\arraybackslash}X}
\newcolumntype{L}{>{\raggedright\arraybackslash}X}
\usepackage[strict]{changepage}
\usepackage[font=footnotesize,
            labelfont=bf,
            labelsep=space,
            skip=1ex]{caption}
\captionsetup[table]{singlelinecheck=false,
                     justification=raggedright}

\begin{document}
    \begingroup                             % <---
\footnotesize\linespread{0.84}\selectfont   % <---
\renewcommand{\arraystretch}{1.2}           % <---
\setlength\tabcolsep{4pt}                   % <---
\newcommand\mcx[1]{\multicolumn{2}%
    {@{}>{\hsize=\dimexpr1.75\hsize+2\tabcolsep}L}{#1}} % <---
\begin{tabularx}{\linewidth}{@{}
                            l               % <---
        >{\hsize=1.75\hsize}L
   *{5}{>{\hsize=0.95\hsize}C}
                             @{}}
\caption{caption}
    \label{resultstable}                    \\
    \toprule
    & \thead[bl]{variables}
        & \thead[b]{1}
            & \thead[b]{Cluster \\ 2 \\(n=111)}
                & \thead[b]{3 \\(n=111)}
                    & \thead[b]{4\\()}
                        & \thead[b]{5\\()}  \\
    \midrule
\endfirsthead
\caption{caption (cont.)}
    \label{resultstable}                    \\
    \toprule
    & \thead[bl]{variables}
        & \thead[b]{1}
            & \thead[b]{Cluster \\ 2 \\(n=111)}
                & \thead[b]{3 \\(n=111)}
                    & \thead[b]{4\\()}
                        & \thead[b]{5\\()}  \\
    \midrule
\endhead
    \midrule
\multicolumn{6}{r}{\textit{continue on the next page}}
\endfoot
    \bottomrule
\endlastfoot
\mcx{some very long variable name without indented text}
            &   &   &   &                   \\
    \addlinespace
\mcx{var2}  &   &   &   &   &               \\
    & varx  & 0.92 (0.13)
                & 0.65 (0.26)
                    & 0.65 (0.19)
                        & 0.49 (0.23)
                            & 0.44 (0.23)   \\
    & vary  & 0.93 (0.12)
                & 0.52 (0.28)
                    & 0.53 (0.21)
                        & 0.23 (0.22)
                            & 0.02 (0.07)   \\
    \addlinespace
\mcx{varz}  &   &   &   &   &               \\
    & varu  &   &   &   &   &               \\
    & varrn &   &   &   &   &               \\
    & No. of product switches in 2 yr, median (IRQ)
            & 0.93 (0.12)
                & 0.52 (0.28)
                    & 0.53 (0.21)
                        & 0.23 (0.22)
                            & 0.02 (0.07)   \\
    & varx  & 0.92 (0.13)
                & 0.65 (0.26)
                    & 0.65 (0.19)
                        & 0.49 (0.23)
                            & 0.44 (0.23)   \\
    \addlinespace
\mcx{var)}  &   &   &   &   &               \\
\end{tabularx}
    \endgroup                               % <---
\end{document}

enter image description here

(red lines indicate text borders)

2
  • Cheers. I have one question regarding second option. How can you make it so that the text below \mcx lines up if it is too long? For example in this picture i.imgur.com/dnvttNx.png How can you make it so "median" is shift to the right?
    – user21398
    Mar 29, 2019 at 17:36
  • 1
    see addendum in my answer.
    – Zarko
    Mar 29, 2019 at 20:00

You must log in to answer this question.

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