如何将浮动元素与网格对齐?

如何将浮动元素与网格对齐?

我尝试使用 John Kormylo 的想法进行网格排版。

看:同一层级的两列文本

命令 \resetbase 对于非浮动对象(如方程式或标题)非常有效。但是如何在浮动对象之后使用它?当我在 \end{figure} 之后直接添加它时,它没有帮助,因为 Latex 通常会将浮动对象移开。

有任何想法吗?

梅威瑟:

\documentclass[fleqn,twocolumn,12pt]{book}% Some compilations required.

\makeatletter
\def\raggedbottom{%
 \def\@textbottom{\vskip \fill}\let\@texttop\relax}
\makeatother

\raggedbottom

\usepackage{geometry,amsmath,showframe,xparse}
\geometry{a4paper,left=15mm,right=15mm, top=26mm, bottom=17mm}

\parindent=0pt
\parskip=\baselineskip
\jot=\baselineskip

\usepackage{tikzpagenodes}
\newlength{\baselineoffset}

\NewDocumentCommand{\resetbase}{}%
 {\strut\tikz[remember picture, overlay]{\pgfextracty{\baselineoffset}%
  {\pgfpointdiff{\pgfpointorigin}{\pgfpointanchor{current page text area}   {north}}}
   \global\baselineoffset=\baselineoffset}%
  \advance\baselineoffset by \dimexpr \baselineskip-\topskip\relax
  \loop\ifdim\baselineoffset>\baselineskip
    \advance\baselineoffset by -\baselineskip
  \repeat
 \vspace{-\baselineoffset}\par}

\usepackage{eso-pic}
\AddToShipoutPicture{%
  \begin{tikzpicture}
    [remember picture, 
     overlay,
     normal lines/.style={black!20, very thin}]
    \draw [style=normal lines, step=\normalbaselineskip]  (current page.south west) grid (current page.north east); 
  \end{tikzpicture}}

\usepackage{lipsum}

\begin{document}

\chapter{Test}

\section{Test}

\resetbase {\bfseries Here we get the correct alignment. No problem after headings.}
\begin{align}
  \sin \alpha & = \sqrt{\sqrt{\frac{\int dx}{\int f(x)dx}}} \\
    \int \frac12 & = 1 \\
  \sin \alpha & = \sqrt{\sqrt{\frac{\int dx}{\int f(x)dx}}}
\end{align}
\resetbase {\bfseries No problem after equations. But I don't know how to align floats.} \lipsum[1] Here the problem begins. 
\begin{figure}
 \centering
{\Huge $\int \frac{dx}{x} = \ln |x| + c$}
\vskip 20pt
{\large\bfseries How can I put a \string\resetbase{} after a float?}
\caption{Integral}
\end{figure}
{\bfseries No alignment! When I write \string\resetbase{} directly after \string\end \{figure\} it does not help because the float is usually moved away by \LaTeX .} \lipsum[1]

\end{document}

结果

我再次尝试使用 John Kormylo 的\inspace。对于非浮动对象,它可以工作,但图形仍然未对齐。

我究竟做错了什么?

梅威瑟:

\documentclass[fleqn,twocolumn,12pt]{book}% Some compilations required.

\makeatletter
\def\raggedbottom{%
  \def\@textbottom{\vskip \fill}\let\@texttop\relax}
\makeatother

\raggedbottom

\usepackage{geometry,amsmath,showframe,calc}
\geometry{a4paper,left=15mm,right=15mm, top=26mm, bottom=17mm}

\usepackage{xparse}

\parindent=0pt
\parskip=\baselineskip
\jot=\baselineskip
\floatsep=\baselineskip
\textfloatsep=\baselineskip
\intextsep=\baselineskip
\abovecaptionskip=\normalbaselineskip
\belowcaptionskip=\normalbaselineskip

\usepackage{tikzpagenodes}
\newlength{\baselineoffset}

\NewDocumentCommand{\resetbase}{}% put baseline a multiple of \baselineskip below top of page
  {\strut\tikz[remember picture, overlay]{\pgfextracty{\baselineoffset}%
    {\pgfpointdiff{\pgfpointorigin}{\pgfpointanchor{current page text area}{north}}}
   \global\baselineoffset=\baselineoffset}%
   \advance\baselineoffset by \dimexpr \baselineskip-\topskip\relax
   \loop\ifdim\baselineoffset>\baselineskip
     \advance\baselineoffset by -\baselineskip
   \repeat
   \vspace{-\baselineoffset}\par}

\usepackage{eso-pic}
\AddToShipoutPicture{%
  \begin{tikzpicture}
    [remember picture, 
      overlay,
      normal lines/.style={black!20, very thin}]
  \draw [style=normal lines, step=\normalbaselineskip]  (current page.south west) grid (current page.north east); 
  \end{tikzpicture}}

\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/\normalbaselineskip\relax
  \fi
  \dimen0=\normalbaselineskip
  \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}


\usepackage{lipsum}

\begin{document}

\chapter{Test}

\section{Test}

\resetbase {\bfseries Here we get the correct alignment. No problem after headings.}
\inspace{0}{%
  \begin{align}
  \sin \alpha & = \sqrt{\sqrt{\frac{\int dx}{\int f(x)dx}}} \\
    \int \frac12 & = 1 \\
  \sin \alpha & = \sqrt{\sqrt{\frac{\int dx}{\int f(x)dx}}}
  \end{align}}
{\bfseries No problem after equations with \string\inspace.} \lipsum[1] Here the problem begins.
\begin{figure}
  \inspace[c]{0}{%
    \centering
  {\Huge $\int \frac{dx}{x} = \ln |x| + c$}
  \vskip 20pt
  {\large\bfseries Wy doesn't this work?}
  \caption{Integral}}
\end{figure}
{\bfseries Still No alignment! What am I doing wrong?} \lipsum[1]

\end{document}

平均能量损失

相关内容