Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Removing Spaces before and after custom commands

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}

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Anyone any suggestions?

>Solution :

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}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading