XeLatex 方程式和行间内容放置

XeLatex 方程式和行间内容放置

我如何确保在以下模板中将方程式和语句准确放置在行之间。它们永远不会越过任何行,始终位于行之间和红色边距的右侧。

在此处输入图片描述

\documentclass[a4paper,12pt]{article}
\usepackage{fontspec}
\usepackage{amsmath}
\usepackage{xcolor}
\usepackage{tikz}
\usepackage{geometry}
\usepackage{lipsum} % Just for dummy text, remove in your actual document
\usepackage{setspace}

% Page geometry
\geometry{
  left=30mm, % Adjusted to accommodate the red line
  top=25mm,
  right=25mm,
  bottom=25mm,
  headheight=0pt, % Adjust as necessary
  headsep=0pt, % Adjust as necessary
  footskip=12mm, % Adjust as necessary
}

% Define colors
\definecolor{lightblue}{RGB}{173,216,230}
\definecolor{lightred}{RGB}{255,204,203}

% Line spacing to match the TikZ lines, adjust the multiplier as needed
\setstretch{1.255} % Adjust this value as needed

% Header and footer
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{} % clear all header and footer fields
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}

% Background setup
\usepackage{eso-pic}
\newcommand\BackgroundPicture{%
    \begin{tikzpicture}[overlay,remember picture]
        \begin{scope}[shift={(current page.south west)}]
            % Draw vertical line
            \draw [line width=1mm, lightred] (25mm,0) -- (25mm,297mm); % A4 paper is 210mm wide and 297mm high
            
            % Draw horizontal lines
            \foreach \i in {1,...,30} {
                \draw [lightblue] (0,\i*9mm) -- (\paperwidth,\i*9mm);
            }
        \end{scope}
    \end{tikzpicture}%
}

% Ensure the background picture is drawn on every page
\AddToShipoutPictureBG{\BackgroundPicture}

\begin{document}

this is to check the placement of the words.

\begin{equation*}
    E=mc^2
\end{equation*}
\begin{align*}
    a &= b\\
    c &= d
\end{align*}

\end{document}

当前模板的输出如下图所示。

答案1

我认为对齐文本很容易,但对齐其他内容则很难。您需要手动调整中间的间距。这是一个例子。我在左边距放了一条红线。如果没有其他类型的内容(例如,方程式、图形、表格),文本将对齐。如您所见,如果您想添加这些内容,您需要手动调整它们,以使下面的文本对齐。

\documentclass[a4paper,12pt]{article}
\usepackage{fontspec}
\usepackage{amsmath}
\usepackage{xcolor}
\usepackage{tikz}
\usepackage{geometry}
\usepackage{lipsum} % Just for dummy text, remove in your actual document
\usepackage{setspace}

% Page geometry
\geometry{
  left=30mm, % Adjusted to accommodate the red line
  top=25mm,
  right=25mm,
  bottom=25mm,
  headheight=0pt, % Adjust as necessary
  headsep=0pt, % Adjust as necessary
  footskip=12mm, % Adjust as necessary
  showframe
}

% Define colors
\definecolor{lightblue}{RGB}{173,216,230}
\definecolor{lightred}{RGB}{255,204,203}

% Line spacing to match the TikZ lines, adjust the multiplier as needed
\setstretch{1.255} % Adjust this value as needed

% Header and footer
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{} % clear all header and footer fields
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}

% Background setup
\usepackage{eso-pic}
\newcommand\BackgroundPicture{%
    \begin{tikzpicture}[overlay,remember picture]
        \begin{scope}[shift={(current page.north west)}]
            % Draw vertical line
            \draw [line width=1mm, lightred] (1in+\hoffset+\oddsidemargin-0.5\pgflinewidth,0) -- (1in+\hoffset+\oddsidemargin-0.5\pgflinewidth,-\paperheight); % A4 paper is 210mm wide and 297mm high
            
            % Draw horizontal lines
            \foreach \i in {0,1,...,\inteval{\textheight/\baselineskip-1}} {
                \draw [lightblue] (0,\dimexpr-25mm-\fpeval{\i*\baselineskip}pt-\topskip) -- (\paperwidth,\dimexpr-25mm-\fpeval{\i*\baselineskip}pt-\topskip);
            }
        \end{scope}
    \end{tikzpicture}%
}

% Ensure the background picture is drawn on every page
\AddToShipoutPictureBG{\BackgroundPicture}

\begin{document}

\lipsum[1][1-5]
\begin{equation*}
    E=mc^2
\end{equation*}\vspace{\dimexpr-0.5em-\baselineskip}
\begin{align*}
    a &= b\\[-2pt]
    c &= d
\end{align*}\vspace{\dimexpr2.5pt-\baselineskip}

\lipsum[1]\vspace{2pt}

{\centering\includegraphics[width=3cm]{example-image}\par}\vspace{3.5pt}

\lipsum[1-2]
\end{document}

在此处输入图片描述

相关内容