为什么 tabularx 中的 \textwidth 比通常更宽?

为什么 tabularx 中的 \textwidth 比通常更宽?

我在使用 tabularx 时遇到了一个问题:我想在文档的标题页上将两张图片并排显示。我为此使用了 tabularx。我的代码是:

\begin{titlepage}
    \begin{tabularx}{\textwidth}{XX}
        \begin{flushleft}
            \vspace{0.6cm}  % adjust the vertical alignment of the images
            \includegraphics[height=1.3cm]{image1}
        \end{flushleft}
        &
        \begin{flushright}
            \includegraphics[height=2.5cm]{image2}
        \end{flushright}
    \end{tabularx}

    % other content of the titlepage

\end{titlepage}

我的问题是,左侧的图像与左边框完美对齐,但右侧的图像位置不正确。它位于页边距上方,看起来好像不起作用\begin{tabularx}{\textwidth}

如果有人能帮助我,那就太好了。提前谢谢

答案1

与 David C. 和 Johannes B. 先前的评论相呼应,您的代码可以简化如下——请注意完全没有tabularxflushleftflushright环境:

\documentclass{article} 
\usepackage[demo]{graphicx} % omit 'demo' option in real document
\begin{document}
\begin{titlepage}

\noindent % <-- this is required
\includegraphics[height=1.3cm]{image1}\hspace{\fill}
\includegraphics[height=2.5cm]{image2}

% other content of the titlepage
\end{titlepage}
\end{document}

如果两个图形需要相对于彼此垂直居中,只需将\includegraphics语句放在不同的minipage环境中:

\documentclass{article} 
\usepackage[demo]{graphicx} % omit 'demo' option in real document
\begin{document}
\begin{titlepage}

\noindent
\begin{minipage}{0.5\textwidth}
\includegraphics[height=1.3cm]{image1}
\end{minipage}% % <-- the first "%" is important
\begin{minipage}{0.5\textwidth}
\raggedleft % shove stuff to right-hand edge of text block
\includegraphics[height=2.5cm]{image2}
\end{minipage}

% other content of the titlepage
\end{titlepage}
\end{document} 

答案2

如果您仍然喜欢使用tabularx...

\documentclass{article}
\usepackage[demo]{graphicx} % omit 'demo' option in real document
\usepackage{tabularx}

%-------------------------------- show page layout, only for test
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%

\begin{document}

\begin{titlepage}
% \renewcommand\tabularxcolumn[1]{m{#1}} % use in case when you like to have vertical centered images
\noindent % for table start at left text border
\begin{tabularx}{\textwidth}{@{}      % removed \tabcolsep on the left
                                X     % image flushed left 
   >{\raggedleft\arraybackslash}X     % image flushed right 
                             @{}}     % removed \tabcolsep on the right
\includegraphics[height=1.3cm]{image1}
    &
\includegraphics[height=2.5cm]{image2}
\end{tabularx}

% other content of the titlepage
\end{titlepage}

\end{document}

在此处输入图片描述

或者当你激活\renewcommand\tabularxcolumn[1]{m{#1}}

在此处输入图片描述

相关内容