表格放置问题:无法让 mdframed 内的表格在页面上水平居中

表格放置问题:无法让 mdframed 内的表格在页面上水平居中
\begin{center}
\begin{mdframed}[userdefinedwidth= 0.8\textwidth, innerbottommargin= 10]
    \begin{table}[H]
    \centering
    \def\arraystretch{1.5}%
        \begin{tabular}{|c|c|}
            \hline
            \rowcolor{gray!20} \textbf{Row operations on I} & \textbf{Row operations on E} \\
            \rowcolor{gray!20} \textbf{producing E} & \textbf{producing I} \\
            \hline
            Exchange row $i$ with $j$ & Exchange row $j$ with $i$ \\
            \hline
            Add $c$ times row $i$ to row $j$ & Add ${-c}$ times row $i$ to row $j$ \\
            \hline
            Multiply row $i$ by $c \neq 0$ & Multiply row $i$ by $\frac{1}{c}$ \\
            \hline 
        \end{tabular}
    \end{table}
\end{mdframed}
\end{center}

答案1

  • 请始终提供 MWE(最小工作示例),一个小但完整的文档,以\documentclass和开头end{document}
  • 由于您的文档前言是未知的,我们只能猜测,您是否加载了与问题相关的软件包或mdframed在加载时使用了适当的选项。
  • 将较新的浮动环境插入到某个框中!
  • framed你的桌子比盒子的宽度宽
  • 使用tabularx˛ 包使表格变窄,并通过以下方式规定表格宽度\linewidth
  • 用于居中framed使用它的选项align=center
\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{tabularx}
    \newcolumntype{C}{>{\centering\arraybackslash}X}
\usepackage[framemethod=TikZ]{mdframed}

\usepackage{lipsum}

\begin{document}
\lipsum[66][1-4]
\begin{mdframed}[userdefinedwidth=0.9\textwidth, 
                 innerbottommargin= 10,
                 align=center]
    \centering
    \def\arraystretch{1.5}%
        \begin{tabularx}{\linewidth}{|C|C|}
            \hline
            \rowcolor{gray!20} 
\textbf{Row operations on I producing E}    & \textbf{Row operations on E producing I} \\
            \hline
Exchange row $i$ with $j$                   & Exchange row $j$ with $i$ \\
            \hline
Add $c$ times row $i$ to row $j$            & Add ${-c}$ times row $i$ to row $j$ \\
            \hline
Multiply row $i$ by $c \neq 0$              & Multiply row $i$ by $\frac{1}{c}$ \\
            \hline
        \end{tabularx}
\end{mdframed}
\lipsum[66]
\end{document}

在此处输入图片描述

附录:

  • 为了使其居中,应该明确地或通过选项mdframe加载包。tikzmdframe[framemethod=TikZ]
  • 离题的建议,只是为了展示使用包mdframe在其中使用和编写表格的更多可能性tabularay
\documentclass{article}
\usepackage{lipsum}         % for dummy text filler

\usepackage{xcolor}
\usepackage{tabularray}
\UseTblrLibrary{amsmat}     % load amsmath package too

\usepackage{mdframed}
\usepackage{tikz}           % for aligning of mdframe in text

\begin{document}
\lipsum[66][1-4]            % some dummy text

\begin{mdframed}[userdefinedwidth=0.8\linewidth,
                 skipabove=\topskip,    skipbelow=\topskip,     % distance from text
                 innerleftmargin=4pt,   innerrightmargin=4pt,
                 linewidth=1pt,         linecolor=blue,  
                 align=center,                                  % tikz package need to be loaded
                 ]
    \begin{tblr}{hlines, vlines,                                % tabularray package need to be loaded              
                 colsep=4pt,
                 colspec = {*{2}{X[c]}},
                 row{1}  = {font=\bfseries, bg=gray!30},
                 rowsep=5pt}
Row operations on I producing E &   Row operations on E producing I     \\
Exchange row $i$ with $j$       &   Exchange row $j$ with $i$           \\
Add $c$ times row $i$ to row $j$&   Add ${-c}$ times row $i$ to row $j$ \\
Multiply row $i$ by $c \neq 0$  &   Multiply row $i$ by $\frac{1}{c}$   \\
    \end{tblr}
\end{mdframed}
or maybe
\begin{mdframed}[userdefinedwidth=0.8\linewidth,
                 skipabove=\topskip,    skipbelow=\topskip,     % distance from text
                 innerleftmargin=4pt,   innerrightmargin=4pt,
                 linewidth=1pt,         linecolor=blue,
                 align=center,                                  % tikz package need to be loaded
                 ]
    \begin{tblr}{hlines, vlines,                                % tabularray package need to be loaded
                 colsep=4pt,
                 colspec = {*{2}{X[l]}},
                 row{1}  = {c, font=\bfseries, bg=gray!30},
                 rowsep=5pt}
Row operations on I producing E &   Row operations on E producing I     \\
Exchange row $i$ with $j$       &   Exchange row $j$ with $i$           \\
Add $c$ times row $i$ to row $j$&   Add ${-c}$ times row $i$ to row $j$ \\
Multiply row $i$ by $c \neq 0$  &   Multiply row $i$ by $\frac{1}{c}$   \\
    \end{tblr}
\end{mdframed}
\lipsum[66]
\end{document}

在此处输入图片描述

相关内容