表格* 右邊框錯位

表格* 右邊框錯位

我知道這可能聽起來很愚蠢,但我是 LaTeX 的初學者。

\begin{table}[H]
    \centering
    \begin{tabular*}{\textwidth}{|c|}
        \hline                                
        \begin{tabular}[c]{@{}l@{}}           
        \textbf{\textbf{Description sommaire}}:        
    \end{tabular} 
    \\ \hline
    \end{tabular*}
\end{table}

我試圖強製表格進入頁面寬度。我用過{tabular*}它並且有效。但現在我遇到了邊界問題。左邊框正常顯示,但右邊框顯示錯誤。

在此輸入影像描述

我的表格只有一列,希望右邊框位於表格的右端。我怎樣才能做到這一點?

答案1

tabular*有一個特殊的語法,必須遵循該語法才能利用其將表格擴展到整個文字寬度的功能。這是所需的語法:

\begin{table}[htbp]
    \centering
    \begin{tabular*}{\textwidth}{|c|@{\extracolsep{\fill}}c|}
        \hline                                          
        \textbf{\textbf{Description sommaire}}: &  \\ \hline
    \end{tabular*}
\end{table}

在此輸入影像描述

否則,只需使用更常見的tabularx環境(我推薦):

\begin{table}[htbp]
    \centering
    \begin{tabularx}{\textwidth}{|c|X|}
        \hline                                          
        \textbf{\textbf{Description sommaire}}: &  \\ \hline
    \end{tabularx}
\end{table}

這給出了相同的結果加上文字換行作為獎勵。

答案2

您是否正在嘗試製作一個類似框架的章節標題?

因為這樣的話,創建一個浮點數 (a table) 可能是個更糟的主意。這可以將標題替換到任何地方。儘管[H]您可以選擇使浮動不浮動,但table環境仍然是完全不必要的。可能您正在使用它來避免表格縮排並添加垂直空格,但這可以在沒有浮動的情況下更好地解決(分別使用\noindent和一些垂直間距命令\bigskip,或設定\parindent為 0pt 或\parskip更多)。

嵌套表格也沒有意義。一個(簡單的)表格就足夠了,或者一個框架框(\fbox{})可以將線寬減去框架框使用的空間,作為 a \makebox, a\parbox或 minipage 環境,可以在部分重新定義中,或者使用 a非常可配置的裝箱包如mdframedtcolorbox,或簡單地寫一些 \vrules 和 \hrules ,而不製作任何盒子。

此範例顯示了眾多替代方案中的 5 種。使用讓你快樂的那個。

姆韋

\documentclass{article}
% Padding of \fbox{} 
\setlength\fboxsep{.5em}

% Some dummy text between solutions, just to see the layout 
\def\loreipsum{Lorem ipsum dolor sit amet, consectetur adipiscing    
elit. Fusce elit lectus, dapibus eget pellentesque eu, ultricies 
vel turpis. Cras quis convallis nibh.}

% for solution 2 only
\makeatletter
\newcommand\fsection{\@startsection{section}{1}{\z@}%
{1em}{1em}{\bfseries\framedbox}}
\makeatother
\newcommand*\framedbox[1]{\noindent%
\fbox{\parbox{\dimexpr\columnwidth-2\fboxsep}{#1}}}

% for solution 5 only
\usepackage{mdframed}


\begin{document}

\loreipsum

% solution 1
\bigskip\hrule
\noindent
\vrule height 12pt depth 6pt width 0.4pt 
{ \bfseries Description sommaire}\hfill\vrule
\hrule\bigskip

\loreipsum

% solution 2
\fsection*{Description sommaire} 

\loreipsum

% solution 3
\bigskip
\noindent\fbox{\makebox[\dimexpr\linewidth-2\fboxsep-2\fboxrule][l]
{\textbf{Description sommaire}}}\bigskip

\loreipsum

% solution 4
\bigskip
{\renewcommand\arraystretch{1.4}\setlength\tabcolsep{.5em}
\noindent\begin{tabular}%
{|p{\dimexpr\linewidth-2\tabcolsep-2\arrayrulewidth}|}\hline
\textbf{Description sommaire} \\\hline
\end{tabular}}
\bigskip

\loreipsum

% solution 5
\begin{mdframed} 
\textbf{Description sommaire}: 
\end{mdframed}

\loreipsum

\end{document}

相關內容