4

I want a macro to be followed by a paragraph without indentation, even if the source includes a space before the next non-space character.

This is fine if the macro takes no argument, but I do not understand how it should be done if it does take an argument.

In the example below, \isbool is just a control. \istable{} and \istab{} both produce the correct output, but I do not think either does so in a reasonable way.

\documentclass{article} \makeatletter \def\NoIndent#1{\noindent#1} \def\istable#1{#1\par\NoIndent} \def\isbool{b\par\noindent} % sylwad david carlisle: https://tex.stackexchange.com/questions/329949/ignore-par-after-the-end-of-a-macro-then-insert-noindent#comment808392_329949 \def\istab#1{#1\@afterindentfalse\@afterheading\par} \makeatother \begin{document} \istable{g} S \hrule \isbool S \hrule \istab{t} S \hrule \end{document} 

right end; wrong means

I'm pretty sure I shouldn't be using \noindent. I'm also fairly sure I shouldn't be using \@afterheading for a non-heading. I know something like this can probably be done with \futurelet or expl3 peeking, but I'm not confident that is the right way to do this in LaTeX.

Ref.: Ignore \par after the end of a macro; THEN insert \noindent is the source of David's comment, but seems specific to headings. The linked code by egreg seems focused on ignoring paragraphs.

However, most of the posts I found are either about retaining or adding spaces following macros, or else about getting rid of them following environments.

Nonetheless, I imagine this is a duplicate of something I've just failed to find.

7
  • 1
    this isn't really correctly tagged macros, but there doesn't seem to be a tag about writing macros as opposed to one for tex's macro processor. there is no command or commands tag, but only some specific ones which don't apply. Commented Jun 25 at 0:27
  • I'm not sure if I've understood your requirements correctly. Does \def\shinynewmacro#1{\par\noindent\ignorespaces#1} meet them? Commented Jun 25 at 1:22
  • 1
    It will probably be better to clarify your question with the actual use case. The @afterheading is probably the best solution even if semantically sounds wrong. It uses everypar but now you can probably rewrite using lthooks and give it a more appropriate name for your use case. Commented Jun 25 at 4:13
  • 2
    Afterheading is almost always better than using noindent, which latex almost never uses Commented Jun 25 at 6:09
  • Defining your macro rather as {#1\par\noindent\ignorespaces} sounds better than the version using \NoIndent which potentially removes braces. Commented Jun 25 at 6:40

1 Answer 1

4

You can go with \@afterheading, but there's a possibly neater method.

\documentclass{article} \usepackage{showframe} \newcommand{\testA}[1]{% #1\par\nopagebreak \AddToHookNext{para/begin}{\OmitIndent}% } \makeatletter \newcommand{\testB}[1]{% #1\par\nopagebreak \@afterindentfalse\@afterheading } \makeatother \begin{document} \testA{g} S New par \medskip \testA{g} S New par \bigskip \testB{g} S New par \medskip \testB{g} S New par \end{document} 

output

With either \testA or \testB there can be an empty line in between the macro and the next (non indented) paragraph, which is not possible with \noindent.

Here's \testC that tests whether the next token is \par (possibly generated by a blank line) and, if it is, the next paragraph will be indented.

\documentclass{article} \usepackage{showframe} \newcommand{\testA}[1]{% #1\par\nopagebreak \AddToHookNext{para/begin}{\OmitIndent}% } \makeatletter \newcommand{\testB}[1]{% #1\par\nopagebreak \@afterindentfalse\@afterheading } \newcommand{\testC}[1]{% #1\par\futurelet\next\test@C } \newcommand{\test@C}{\ifx\next\par\else\noindent\expandafter\ignorespaces\fi} \makeatother \begin{document} \testA{g} S New par \medskip \testA{g} S New par \bigskip \testB{g} S New par \medskip \testB{g} S New par \bigskip \testC{g} S New par \medskip \testC{g} S New par \end{document} 

with indent

7
  • thanks. why put the \par first? it seems backwards and I don't understand why. Commented Jun 25 at 9:29
  • @cfr The macro should end a paragraph, shouldn't it? Commented Jun 25 at 9:32
  • it would actually be preferable if a blank line restored indentation. it isn't the end of the world if it doesn't, but it would be better if not. Commented Jun 25 at 9:32
  • yes, but why doesn't the \par go after the hook or after the \@afterheading. Commented Jun 25 at 9:33
  • @cfr I don't see why \par should go there. Anyway, I added \testC. Commented Jun 25 at 9:47

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.