4

Consider the following MWE.

\documentclass{minimal}

\usepackage{lipsum}

\newcommand*{\mynoindent}{\noindent}
\newcommand*{\yournoindent}[1]{\noindent}

\begin{document}

\noindent
\lipsum[1]

\mynoindent
\lipsum[1]

\yournoindent{stuff}
\lipsum[1]

\end{document}

This outputs the following. Output Here are close-ups of the relevant indentations (the first two are identical, and so I only show one of those). \noindent \yournoindent

If you look closely, you can see that the second is indented every so slightly. How can I go about fixing this?

(Of course, in this example, I don't actually use the parameter, but for what I want to do, I need to be able to send a parameter to a command that ends in a \noindent.)

  • you should almost never use \noindent in the definition of a top level latex command anyway (it is only used in one place in the latex format, in \@hangfrom which isn't really a latex command, just a remnant of plain tex. – David Carlisle Jul 11 '16 at 9:09
  • @DavidCarlisle You mean like how you should almost never use \def in a top-level LaTeX command? In this case, there is the LaTeX \newcommand. What is the LaTeX version of \noindent? – Jonathan Gleason Jul 11 '16 at 13:54
  • No, \newcommand resolves to \def anyway so if you know what you are doing and know it is safe to omit some checks then \def works. But \noindent (as demonstrated here) almost always does the wrong thing. if you look where indentation is suppressed eg after a section heading, latex never uses \noindent (and here you should probably be using the same \@afterheading hook to control that). Unlike \def where latex uses \def wrapped in \newcommand latex really doesn't use \noindent at all. – David Carlisle Jul 11 '16 at 13:58
  • @DavidCarlisle What is the 'correct' way to go about doing this then? The effect I am going for in practice is not unlike the effect with \section. More specifically, I have a command \Step that takes a single argument and outputs "Step <counter>: <argument>". It then ends the paragraph, and finally ends with a \noindent (and now \noindent\ignorespaces) (it also of course increases the counter). – Jonathan Gleason Jul 11 '16 at 14:04
  • 1
    I'd probably define it using \@startsection (basically a copy of say subsubsection from article.cls but with modified formatting of the argument and number) – David Carlisle Jul 11 '16 at 14:15
7

Use

\newcommand*{\yournoindent}[1]{\noindent\ignorespaces}

which should gobble (or ignore) the space following \yournoindent{<stuff>}.

  • Thanks! This works. Could you explain why though? Naively looking at the code, there doesn't seem to be any more spaces in the third case than there are in the first and second. – Jonathan Gleason Jul 11 '16 at 4:17
  • 1
    @JonathanGleason: The is a difference between a macro with an argument and one without. Try \newcommand\abc}{} \newcommand{\ABC}[1]{} and then write This\abc that. This\ABC{stuff} that. The former gobbles the space between This and that. The latter preserves it. – Werner Jul 11 '16 at 4:19
  • 2
    @JonathanGleason: Also see Space after LaTeX commands. – Werner Jul 11 '16 at 4:29
  • 1
    \newcommand\abc}{} is there an extra brace there? – A Feldman Jul 11 '16 at 9:45
  • 1
    @AFeldman: There is, and I can't edit my comments anymore. Was supposed to just be \newcommand{\abc}{} or \newcommand\abc{}, not \newcommand\abc}{}. – Werner Jul 11 '16 at 15:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.