调整宽度无法将 ltablex 表向左移动

调整宽度无法将 ltablex 表向左移动

我想将表格稍微向页面左侧移动(超出左侧文本宽度边界)。经过多次谷歌搜索,我仍然遇到问题,无法让 ˙\adjustwidth` 正常工作:

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

它完美地将表格向右扩展,但不会将左侧扩展。但是,它确实将标题向左扩展,但不会将整个表格扩展?有人知道这是为什么吗?是不是因为使用了太多软件包?

答案1

您的表格存在许多问题:

  • ltablex不能很好地配合\adjustwidth。您的表格真的有多页长吗?
  • 仅当单元格的内容比列的标准宽度宽时才使用ltablex保留列带,否则如果有列带,它会将列宽缩小到宽度。解决方法是添加到文档序言中XXl\keepXColumns
  • 不管您的表格中是否只有一列内容(其他列为空),您都需要写出所有与号。添加遗漏的与号可以解决您的问题(如果您不使用ltablex但很简单tabularx)。
  • \adjustwidth不是为了改变更多页面的文本宽度。因为这更适合\newgeometrygeometry包中使用。

考虑以上评论,该表可以写成:

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

这使:

在此处输入图片描述

如果你把它写得Very very very very Long variable name更短,例如使用缩写或者把它分成两行或更多行,那么\adjustwidth就不再需要使用了。在这种情况下,ltablex此表的用法将按预期工作:

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

在此处输入图片描述

附录: 根据您下面的评论,(可能的)解决方案是在表中添加一列并重新设计命令\mcx。我还建议用\mcx命令在行前添加更多垂直空间(所有更改都在代码中指示% <---):

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

在此处输入图片描述

(红线表示文本边框)

相关内容