将缩进设置为 `0em`,使用换行符时,仍然会有一个空格大小的缩进

将缩进设置为 `0em`,使用换行符时,仍然会有一个空格大小的缩进

问题描述

我对 *TeX 还很陌生,但我将一些\newcommands 定义为某种段落样式(可能不是正确的方法,也许有人可以告诉我如何更好地做到这一点)。

我想要默认的段落样式,没有缩进,段后空格应为 0.08 英寸,段前空格应为 0 英寸。但是,任何段落样式都可以更改这些设置中的任何一个,甚至更多(例如字符样式,例如粗体、斜体、下划线等)。

现在,有时我想插入特定段落样式的段落没有段落后面留空。为此我使用换行符 ( \\),这通常可以完成工作。但是,对于\paragraphStyle下面的代码,这却不适用。

似乎在第一段中,有一个空格大小的缩进,无论我做什么,都无法消除它。

我尝试过的

我尝试以多种不同的组合来使用\noindent和。\setlength{\parindent}{0em}

最小工作示例

%%%%%  Preamble  %%%%%
\documentclass[10pt]{book}
\usepackage[a4paper]{geometry}
\geometry{
    a4paper,
    left=1in,
    right=1in,
    top=1in,
    bottom=1in,
    portrait
}

\usepackage{xcolor}

% Font family
\usepackage[math-style=ISO, bold-style=ISO, partial=upright, nabla=upright]{unicode-math}
\setmainfont{Libertinus Serif}

% Paragraph and line settings
\setlength{\parindent}{0em}           % Set paragraph indentation
\setlength{\parskip}{0.08in}          % Paragraph spacing
\renewcommand{\baselinestretch}{1.0}  % Line \expandafter\selectlanguage\expandafter{\cvlang}

% Custom commands
\newcommand{\paragraphStyle}[1]{\setlength{\parindent}{0pt}\setlength{\parskip}{0.16in}\renewcommand{\baselinestretch}{2.0}\color{black!100}\fontsize{20pt}{24pt}\selectfont{\textbf{#1}}\color{black!100}\normalsize\setlength{\parindent}{0em}\setlength{\parskip}{0.08in}\selectfont\renewcommand{\baselinestretch}{1.0}}%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}
    \pagestyle{empty}

    This is wrong:

    \paragraphStyle{
        First line\\
        Second line
    }

    This is correct:

    \paragraphStyle{First line}\\
    \paragraphStyle{Second line}


    Also this is correct in indentation, but not in space after paragraph:

    First Line\\
    Second Line
\end{document}

输出

在此处输入图片描述

答案1

正如 Marcel 指出的那样,您的输入中有一个多余的空格。您可以按照 Marcel 的建议删除该空格,或者您可以添加\ignorespaces在你的宏定义中,对于 的论证\textbf,在 之前#1。另外, 没有必要\textbf{}放在它自己的括号中,所以我把它删掉了。

注意:现在定义的那部分\textbf{\ignorespaces#1}也可以替换为{\bfseries#1}。这里,括号现在是限制范围所必需的,但\ignorespaces不再是必需的因为会将您置于垂直模式,因此会忽略\bfseries中的前导空格,而会将您置于水平模式,其中前导空格很重要#1\textbf{}

此外,我重新组织了您的定义,\paragraphStyle使其更易于人们阅读。请注意%行尾分隔符的存在,以避免再次出现同样的问题。

\documentclass[10pt]{book}
\usepackage[a4paper]{geometry}
\geometry{
    a4paper,
    left=1in,
    right=1in,
    top=1in,
    bottom=1in,
    portrait
}

\usepackage{xcolor}

% Font family
%\usepackage[math-style=ISO, bold-style=ISO, partial=upright, nabla=upright]{unicode-math}
%\setmainfont{Libertinus Serif}

% Paragraph and line settings
\setlength{\parindent}{0em}           % Set paragraph indentation
\setlength{\parskip}{0.08in}          % Paragraph spacing
\renewcommand{\baselinestretch}{1.0}  % Line \expandafter\selectlanguage\expandafter{\cvlang}

% Custom commands
\newcommand{\paragraphStyle}[1]{%
  \setlength{\parindent}{0pt}%
  \setlength{\parskip}{0.16in}%
  \renewcommand{\baselinestretch}{2.0}%
  \color{black!100}%
  \fontsize{20pt}{24pt}%
  \selectfont%
  \textbf{\ignorespaces#1}%
  \color{black!100}%
  \normalsize%
  \setlength{\parindent}{0em}%
  \setlength{\parskip}{0.08in}%
  \selectfont%
  \renewcommand{\baselinestretch}{1.0}%
}%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}
    \pagestyle{empty}

    This is no longer wrong:

    \paragraphStyle{
        First line\\
        Second line
    }

    This is correct:

    \paragraphStyle{First line}\\
    \paragraphStyle{Second line}


    Also this is correct in indentation, but not in space after paragraph:

    First Line\\
    Second Line
\end{document}

在此处输入图片描述

答案2

“缩进一个空格的大小”实际上并不是缩进,而是行首的一个空格:

看着

    This is wrong:

    \paragraphStyle{
        First line\\
        Second line
    }

后面的换行符\paragraphStyle{被转换为空格,从而导致观察到的“缩进”。您可以通过添加注释%来注释换行符,从而抑制空格,从而避免这种情况:

    This is wrong:

    \paragraphStyle{%
        First line\\
        Second line
    }

完整文档变为

%%%%%  Preamble  %%%%%
\documentclass[10pt]{book}
\usepackage[a4paper]{geometry}
\geometry{
    a4paper,
    left=1in,
    right=1in,
    top=1in,
    bottom=1in,
    portrait
}

\usepackage{xcolor}

% Font family
\usepackage[math-style=ISO, bold-style=ISO, partial=upright, nabla=upright]{unicode-math}
\setmainfont{Libertinus Serif}

% Paragraph and line settings
\setlength{\parindent}{0em}           % Set paragraph indentation
\setlength{\parskip}{0.08in}          % Paragraph spacing
\renewcommand{\baselinestretch}{1.0}  % Line \expandafter\selectlanguage\expandafter{\cvlang}

% Custom commands
\newcommand{\paragraphStyle}[1]{\setlength{\parindent}{0pt}\setlength{\parskip}{0.16in}\renewcommand{\baselinestretch}{2.0}\color{black!100}\fontsize{20pt}{24pt}\selectfont{\textbf{#1}}\color{black!100}\normalsize\setlength{\parindent}{0em}\setlength{\parskip}{0.08in}\selectfont\renewcommand{\baselinestretch}{1.0}}%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}
    \pagestyle{empty}

    This is wrong:

    \paragraphStyle{%
        First line\\
        Second line
    }

    This is correct:

    \paragraphStyle{First line}\\
    \paragraphStyle{Second line}


    Also this is correct in indentation, but not in space after paragraph:

    First Line\\
    Second Line
\end{document}

答案3

您应该使用分组来隔离字体和行距的更改,这样您就不必事后恢复它们。但是,由于必须进行这些更改,因此您需要结束前一个段落(如果尚未完成),并且还要在特殊设置的末尾结束一个段落,以免影响后面的段落。

%%%%%  Preamble  %%%%%
\documentclass[10pt]{book}
\usepackage[a4paper]{geometry}
\geometry{
    a4paper,
    left=1in,
    right=1in,
    top=1in,
    bottom=1in,
    portrait,
}

\usepackage{xcolor}

% Font family
\usepackage[math-style=ISO, bold-style=ISO, partial=upright, nabla=upright]{unicode-math}
\setmainfont{Libertinus Serif}

% Paragraph and line settings
\setlength{\parindent}{0em}           % Set paragraph indentation
\setlength{\parskip}{0.08in}          % Paragraph spacing
\renewcommand{\baselinestretch}{1.0}  % Line 

% Custom commands
\newcommand{\paragraphStyle}[1]{%
  \par
  \begingroup
  \setlength{\parindent}{0pt}%
  \setlength{\parskip}{0.16in}%
  \renewcommand{\baselinestretch}{2.0}%
  \fontsize{20pt}{24pt}\selectfont
  \noindent\color{black!100}%
  \bfseries\ignorespaces #1\par
  \endgroup
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}

\pagestyle{empty}

This is right:

\paragraphStyle{
  First line\\
  Second line
}

Also this is correct in indentation, but not in space after paragraph:

First Line\\
Second Line

\end{document}

在此处输入图片描述

相关内容