我正在嘗試創建一個具有兩個“列”的佈局,其中右側列本身有兩列,分別右對齊和左對齊。例如:
This is some text in the first Label Foo
column. Another Label Foo Bar Baz
我熟悉透過minipage
環境在同一行上創建左對齊和右對齊文字的技術,因此擴展了這個想法,我設置了右側列以包含 atabular
來排列其標籤和文字。這是我所擁有的:
\documentclass[letterpaper,12pt]{report}
\usepackage[margin=1in]{geometry}
\begin{document}
\noindent
\begin{minipage}[t]{.49\textwidth}
\flushleft
Some long testing text to illustrate the alignment problem.
\end{minipage}
%
\hfill
%
\noindent
\begin{minipage}[t]{.49\textwidth}
\flushright
\begin{tabular}{r l}
\textbf{Some Long Label} & Bar \\
\textbf{Another Long Label} & Foo Bar Baz \\
\end{tabular}
\end{minipage}
\end{document}
這編譯並大多有效,但有一個問題除外:表格中文字的頂部看起來比左側小頁中文字的頂部稍高。我相信這是因為tabular
在它們之前和之後自然有一些額外的垂直空間,但我不知道如何解決這個問題。
我的問題是,如何修復我的程式碼以使每行中的文字行minipage
垂直向上,或者是否有一些更乾淨的方法來創建此佈局而不使用tabular
?
答案1
[t]
您也忘記在以下位置使用tabular
:
\documentclass[letterpaper,12pt]{report}
\usepackage[margin=1in]{geometry}
\begin{document}
\noindent
\begin{minipage}[t]{.49\textwidth}
\raggedright
Some long testing text to illustrate the alignment problem.
\end{minipage}% <-- Don't forget this one
%
\hfill
%
\begin{minipage}[t]{.49\textwidth}
\raggedleft
\begin{tabular}[t]{@{} r l @{}}% <-- Don't forget @{}!
\textbf{Some Long Label} & Bar \\
\textbf{Another Long Label} & Foo Bar Baz \\
\end{tabular}
\end{minipage}
\end{document}
切勿使用\flushleft
和\flushright
作為命令:它們的存在只是因為存在環境flushleft
和flushright
。要使用的命令是\raggedright
和\raggedleft
。
更簡單的方法是tabular*
:
\documentclass[letterpaper,12pt]{report}
\usepackage[margin=1in,showframe]{geometry}
\begin{document}
\noindent
\begin{tabular*}{\textwidth}{@{}p{.45\textwidth}@{\extracolsep{\fill}}r@{}}
\raggedright
Some long testing text to illustrate the alignment problem.
&
\begin{tabular}[t]{@{}r l@{}}
\textbf{Some Long Label} & Bar \\
\textbf{Another Long Label} & Foo Bar Baz \\
\end{tabular}
\end{tabular*}
\end{document}
我添加showframe
只是為了顯示邊距。
答案2
這是一個將每個設定minipage
為 a 的解決方案tabularx
:
\documentclass{article}
\usepackage{tabularx}% http://ctan.org/pkg/tabularx
\begin{document}
Some text before.
\noindent
\begin{tabularx}{.5\linewidth}[t]{@{}X@{}}
Some long testing text to illustrate the alignment problem.
\end{tabularx}%
\begin{tabularx}{.5\linewidth}[t]{%
>{\raggedleft\bfseries}p{.3\linewidth}
>{\raggedright\arraybackslash}X@{}}
Some Long Label & Bar \\
Another Long Label & Foo Bar Baz
\end{tabularx}%
Some text after.
\end{document}
使用以下方式指定每列的對齊方式array
包裹接口(由載入tabularx
)。
請注意,這些區塊不會跨越頁面邊界。