動態方法(相對字體大小)

動態方法(相對字體大小)

如何固定文字和行之間的垂直間距?在下面的 MWE 中,文字與其下方的行之間的垂直間距遠大於文字與其上方的行之間的垂直間距。我希望這兩個垂直空間相等,並且我希望能夠為該垂直空間賦予一個值。

\documentclass[12pt]{report}

\begin{document}

\rule{6cm}{0.4pt}\par
\textbf{\large TITLE}\par
\rule{8cm}{0.4pt}

\end{document}

答案1

規則繪製在文本的基線處。因此,下面的行位於下一行文字的基線處。

在此輸入影像描述

所以你必須將下線提升適當的量。這可以透過\vspace將深度添加到\rule.

\documentclass[12pt]{report}

\begin{document}

\rule{6cm}{0.4pt}Some\par
\textbf{\large TITLE}\par%\vspace{-0.66\baselineskip}
\rule[0.66\baselineskip]{8cm}{0.4pt}Some

\end{document}

在此輸入影像描述

答案2

我已經掌握了這個概念,並有一個很好地說明了這一點的解決方案:

動態方法(相對字體大小)

意識到在每條新行之後,線條的繪製都從字母所在的位置(字形的基線)開始,因此您將線條抬高到文字下方。多少? user11232 使用了 60% 的基線跳過。另一種方法是直接使用某個大寫字母的高度,例如\fontcharht\font"004D將得到 M 的高度(以磅為單位)。這\baselineskip是由班級設定的\@setfontsize,請參閱\baselineskip 是自動定義的嗎?

例如,如果您因字母寬度深度而對光學結果不滿意,您甚至可以將其設定為大寫字母高度的 70%(只需.7在 前面添加\fontcharht)。

在此輸入影像描述

初步方法

在文字上方和下方畫一條線,看看會發生什麼。結果在視覺上並不令人滿意。

在此輸入影像描述

完整程式碼

\documentclass{article}
\usepackage{fontspec}% xelatex

% Note that you can remove all the \noindent occurrences
% with \setlength\parindent{0pt} which sets empty indent box
% to 0pt globally

\newcommand\linedatWRONG[1]{% comment out this line-ending
  \leavevmode\par\noindent\rule{4cm}{0.4pt}gello\par\noindent%
  #1\rule{1cm}{0.4pt}\par\noindent%
  \rule{4cm}{0.4pt}gello\par%
}

\newcommand\linedatRIGHT[1]{% comment out this line-ending
  \leavevmode\par\noindent\rule{4cm}{0.4pt}gello\par\noindent%
  #1\rule{1cm}{0.4pt}\par\noindent%
  \rule[\fontcharht\font`M]{4cm}{0.4pt}gello\par% raise line up by the height of M in the current font
}

\newdimen\Mheight % for demo only
\Mheight=\fontcharht\font`M% for demo only

\begin{document}
\noindent The \texttt{\textbackslash linedatWRONG} version does indeed have the correct
alignment according to TeX's rules of baseline skips. I used the word
{\char"201C}gello{\char"201D} to demonstrate that the lines are at the baselines.
\linedatWRONG{Hello gello}% purposely used glyph with depth "g"
\vspace{2\baselineskip}
\noindent In the \texttt{\textbackslash linedatRIGHT}, we raise the bottom line up by the height of an upper case {\char"201C}M{\char"201D}
in the current font, which happens to be \the\Mheight. Programming this dynamically ensures
that the value of M will be ajusted to the current scope's font.
\linedatRIGHT{Hello gello}% purposely used glyph with depth "g"
\end{document}

筆記

  • 警告當基線跳躍略大於字元的深度+高度時,此方法有效。如果基線跳躍特別大,那麼與基線跳躍相比,將底線提高字元的深度+高度可能看起來微不足道。
  • Article 類別指定字型大小 10pt ( \@xpt) 具有關聯的 12pt ( \@xiipt) 基準跳過。
  • 另一個選擇是使用線的深度

相關內容