如何将表格定位在右边距和当前行的准确高度?

如何将表格定位在右边距和当前行的准确高度?

我试图将表格定位为使得表格顶部与其旁边的文本具有相同的高度,但总是有一条非常烦人的线,我似乎无法摆脱它。

基本上,我只想让表格向上移动一行的高度。

\item \begin{minipage}[t]{0.5\textwidth} Multiply the value of each region with its
corresponding probability,  then sum up the results. \end{minipage} \begin{minipage}
[t]{4cm} \begin{tabular}[t]{|r|l|} \hline Probability & Region \\ \hline 0.0077 & -3 \\
\hline 0.0462 & -2 \\ \hline 0.2462 & -1 \\ \hline 0.4077 & 0 \\ \hline 0.2385 & 1 \\
\hline 0.0462 & 2 \\ \hline 0.0077 & 3 \\ \hline \end{tabular} \end{minipage}

答案1

您可以adjustbox按照以下方式使用该包:

\documentclass{article}
\usepackage{array}
\usepackage{adjustbox}
\begin{document}
\begin{enumerate}
    \item test
    \item \begin{minipage}[t]{0.5\textwidth} 
          Multiply the value of each region with its corresponding probability,  then sum up the results. 
          \end{minipage}
          \begin{adjustbox}{valign=t}                % Comment if using the option below.
          %\begin{adjustbox}{valign=t,raise=0.125ex} % Uncomment to get the baseline of the text and the first line of the table aligned.
          \begin{tabular}{|r|l|} 
          \hline Probability & Region \\ \hline 
          0.0077 & -3 \\ \hline 
          0.0462 & -2 \\ \hline 
          0.2462 & -1 \\ \hline 
          0.4077 & 0 \\ \hline 
          0.2385 & 1 \\ \hline 
          0.0462 & 2 \\ \hline 
          0.0077 & 3 \\ \hline 
          \end{tabular}
          \end{adjustbox}
\end{enumerate}
\end{document}

在此处输入图片描述

答案2

我省略了表格顶部、底部、左侧和右侧的行。然后使用[t]tabular 的选项强制在第一行对齐。最后,使用 将周围的行放回原处\fbox,这样可以保留基线:

\documentclass{article}
\begin{document}
\begin{enumerate}
\item test
\item \begin{minipage}[t]{0.5\textwidth}
        Multiply the value of each region with its corresponding
        probability,  then sum up the results.
      \end{minipage}
{\fboxsep0pt
\fbox{\begin{tabular}[t]{r|l}
    Probability & Region \\ \hline
    0.0077 & -3 \\ \hline
    0.0462 & -2 \\ \hline
    0.2462 & -1
  \end{tabular}}}
\end{enumerate}
\end{document}

相关内容