移除自定义命令前后的空格。

huangapple go评论59阅读模式
英文:

Removing Spaces before and after custom commands

问题

I can help you with the translation. Here's the translated content you provided:

我在使用Latex和自定义命令时遇到了问题。自定义命令\refChap 每次使用时都会在插入的文本前后添加空格。我希望有一个解决方案来取消这些空格。

下面的示例应该详细说明了我的问题:

\documentclass[11pt]{article}

\usepackage[a4paper,
                left=2cm,
                right=2cm,
                top=2cm,
                bottom=2cm]{geometry}
\setlength{\parskip}{6 pt} % 段落间距 6 pt
\setlength{\parindent}{0pt} % 段落开头无缩进

\usepackage{nameref}

\newcommand\Section[1]{ %
    \section{#1}
    \label{#1}
}

\newcommand\refChap[1]{
Chapter \ref{#1} (\nameref{#1} on p.\pageref{#1})
}

\begin{document}
当我引用\refChap{Header}时,我希望插入的文本前后都有一个空格。然而,该命令会在该命令前后各添加一个空格,导致在单词“Chapter”之前有两个空格,逗号之前有一个空格。

当我在\refChap{Header}前后不添加任何空格时,这种行为更为明显,我希望最终的结果中也没有空格。但实际上有空格。

\Section{Header}

我希望有一个解决方案来取消这些额外的空格,以便

\textit{当我引用\textbackslash{}refChap\{Header\}时,我期望 [...]}

看起来像:

\textit{当我引用Chapter 1 (Header on p.1)时,我期望 [...]}
\end{document}

Please note that the LaTeX code remains the same, and only the text is translated.

英文:

I have a problem using Latex and custom commands. The custom command \refChap adds spaces before and after the inserted text every time when used. I would like a solution that suppresses said spaces.

The following example should explain my problem in depth:

\documentclass[11pt]{article}

\usepackage[a4paper,
            left=2cm,
            right=2cm,
            top=2cm,
            bottom=2cm]{geometry}
\setlength{\parskip}{6 pt} % paragraph 6 pt
\setlength{\parindent}{0pt} % no indent at paragraph start

\usepackage{nameref}



\newcommand\Section[1]{ %
	\section{#1}
	\label{#1}
}

\newcommand\refChap[1]{
Chapter \ref{#1} (\nameref{#1} on p.\pageref{#1})
}

\begin{document}
When I reference \refChap{Header}, I'd expect there to be exactly one space before and none after the inserted text. However, the command will add one space each before and after the command, resulting in two spaces being before the word \"{}Chapter\"{} and one before the comma.

This behavior can be better seen when I do not add any spaces before and after\refChap{Header}, as I would expect there to be no space in the final result as well. However, there are.

\Section{Header}

I would like to have a solution to surpress those additional spaces, so that 

\textit{When I reference \textbackslash{}refChap\{Header\}, I'd expect [...]}

would look like:

\textit{When I reference Chapter 1 (Header on p.1), I'd expect [...]}
\end{document}

移除自定义命令前后的空格。

Anyone any suggestions?

答案1

得分: 1

以下是您提供的内容的翻译:

额外的空格是由于未受保护的行尾引起的,它们会像空格一样起作用。为了避免这种情况,在行的末尾加上 %

\documentclass[11pt]{article}

\usepackage[a4paper,
            left=2cm,
            right=2cm,
            top=2cm,
            bottom=2cm]{geometry}
\setlength{\parskip}{6 pt} % 段落间距 6 pt
\setlength{\parindent}{0pt} % 段落开头无缩进

\usepackage{nameref}

\newcommand\Section[1]{%
    \section{#1}%
    \label{#1}%
}

\newcommand\refChap[1]{%
Chapter \ref{#1} (\nameref{#1} on p.\pageref{#1})%
}

\begin{document}
当我引用 \refChap{Header} 时,我期望在插入的文本之前有一个空格,之后没有空格。然而,该命令将在命令之前和之后各添加一个空格,导致单词“Chapter”之前有两个空格,逗号之前有一个空格。

当我在\refChap{Header} 前后不添加任何空格时,这种行为更加明显,我期望最终结果中也没有空格。然而,实际上有空格。

\Section{Header}

我希望有一种解决方法来消除这些额外的空格,以便

\textit{当我引用 \textbackslash{}refChap\{Header\} 时,我期望 [...]}

看起来像:

\textit{当我引用 Chapter 1 (Header on p.1) 时,我期望 [...]}
\end{document}

请注意,这是提供的文本的翻译,其中包括 LaTeX 代码和相应的注释。如果您需要进一步的信息或有其他要求,请告诉我。

英文:

The additional spaces are caused by the unprotected line endings, which will act like a space. To avoid this, place a % at the end of lines:

\documentclass[11pt]{article}

\usepackage[a4paper,
            left=2cm,
            right=2cm,
            top=2cm,
            bottom=2cm]{geometry}
\setlength{\parskip}{6 pt} % paragraph 6 pt
\setlength{\parindent}{0pt} % no indent at paragraph start

\usepackage{nameref}



\newcommand\Section[1]{%
    \section{#1}%
    \label{#1}%
}

\newcommand\refChap[1]{%
Chapter \ref{#1} (\nameref{#1} on p.\pageref{#1})%
}

\begin{document}
When I reference \refChap{Header}, I'd expect there to be exactly one space before and none after the inserted text. However, the command will add one space each before and after the command, resulting in two spaces being before the word \"{}Chapter\"{} and one before the comma.

This behavior can be better seen when I do not add any spaces before and after\refChap{Header}, as I would expect there to be no space in the final result as well. However, there are.

\Section{Header}

I would like to have a solution to surpress those additional spaces, so that 

\textit{When I reference \textbackslash{}refChap\{Header\}, I'd expect [...]}

would look like:

\textit{When I reference Chapter 1 (Header on p.1), I'd expect [...]}
\end{document}

huangapple
  • 本文由 发表于 2023年5月22日 20:09:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76306051.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定