将标题设置为 \baselineskip 的倍数以进行网格排版

将标题设置为 \baselineskip 的倍数以进行网格排版

我想在网格上排版我的文档,因此我使用以下命令作为标题:

\titleformat{\section}
{\fontsize{20}{2\baselineskip}\selectfont\bfseries\sffamily}{}{0em}{#1}
\titlespacing*{\section}{0pt}{0pt}{0pt}

但有些事情失败了

在此处输入图片描述

我怎样才能解决这个问题?

完整 MWE:

\documentclass[10pt,twocolumn]{book}
\usepackage{calc}
\usepackage{geometry}
\usepackage{ragged2e}
\usepackage[explicit]{titlesec}
\usepackage{lipsum}

\usepackage{fontspec}
\setmainfont{Cambria}
\setsansfont{Calibri}

\pagestyle{empty}

\geometry{showframe}

\setlength{\parskip}{0pt}

\titleformat{\section}
{\fontsize{20}{2\baselineskip}\selectfont\bfseries\sffamily}{}{0em}{#1}
\titlespacing*{\section}{0pt}{0pt}{0pt}

\begin{document}

    \raggedbottom\RaggedRight

    \section{First section}

    \lipsum

\end{document}

答案1

该宏将\inspace通常无法容纳在固定行数中的内容覆盖在固定行数上。您可以指定行数,也可以基于框的大小计算行数。选项 t/c/b 将框的顶部/中心/底部与保留空间对齐。(除 t 和 b 之外的任何内容都被视为 c。)

值得注意的是,第一个\section比第二个大得多。

\documentclass[10pt,twocolumn]{book}
\usepackage{calc}
\usepackage{geometry}
%\usepackage{ragged2e}
\usepackage[explicit]{titlesec}
\usepackage{lipsum}

\usepackage{fontspec}
\setmainfont{Cambria}
\setsansfont{Calibri}

\usepackage{everypage}
\usepackage{tikzpagenodes} 
\AddEverypageHook{%
  \begin{tikzpicture}[overlay,remember picture]
    \draw[color=red,very thin]
      (current page text area.south west) grid[step=\normalbaselineskip] 
      (current page text area.north east);
  \end{tikzpicture}%
}

\pagestyle{empty}

\geometry{showframe}

\setlength{\parskip}{0pt}

\newcommand{\inspace}[3][c]% #1 = t/c/b (optional), #2 = number of lines (0=compute), #3 = contents
{\bgroup
  \setbox0=\vbox{#3}%
  \count1=#2\relax
  \def\test{#1}%
  \ifnum\count1<1
    \dimen0=\dimexpr \ht0+\dp0\relax
    \count1=\numexpr \dimen0/\baselineskip\relax
  \fi
  \dimen0=\baselineskip
  \multiply\dimen0 by \count1
  \ifvmode
    \noindent
    \let\terminate=\par
  \else
    \newline
    \let\terminate=\newline
  \fi
  \def\opt{t}% align to top
    \ifx\test\opt\relax
    \strut\rlap{\raisebox{\dimexpr \ht\strutbox-\ht0}[0pt][0pt]{\usebox0}}\terminate
  \else
    \def\opt{b}% align to bottom
    \ifx\test\opt\relax
      \strut\rlap{\raisebox{\dimexpr \ht\strutbox+\dp0-\dimen0}[0pt][0pt]{\usebox0}}\terminate
    \else% anything else is centered
      \strut\rlap{\raisebox{\dimexpr \ht\strutbox-0.5\ht0+0.5\dp0-0.5\dimen0}[0pt][0pt]{\usebox0}}\terminate
    \fi
  \fi
  \loop\ifnum\count1>1
    \advance\count1 by -1
    \strut\terminate
  \repeat
\egroup}


\begin{document}

    \raggedbottom%\raggedright

    \inspace[b]{3}{\section{First section}}

    \lipsum[1-4]

    \inspace[b]{3}{\section{Second section}}

    \lipsum[5-7]

\end{document}

相关内容