Because I write my own document class (okay, I try to ...), this question bothers me for weeks as there are many situation, in which I need something like this.
Is there a way to test, if the next token list in the input stream is command \foo
? I didn't found anything like this (or I didn't recognize it), neither in the web nor in the expl3
documentation. For example, I want to realize something like this:
\DeclareDocumentCommand \test { } {%
\if_next_tl:NTF \emph { % This function doesn't exist!
% Do something if true
}{
% Do something if false
}
}
So when I write
Bla \test Bla Bla
the false code is executed, and when I write
Bla \test \emph{Bla} Bla
the true code is executed.
I know there are LaTeX3 functions like \peek_after:Nw
, \peek_charcode:NTF
and TeX macros like \@ifnextchar
, but I don't understand how to use them in this case. I think it would be the best to use LaTeX3 functionality for this. And maybe, I am completely wrong and this is not the correct attempt to solve things in LaTeX.
In addition: Is there a similar way to test, if there follows an environment foo
in the input stream?
Background
This is an simplified example where I could use this: I have a command \exercise
, which typesets something like an headline with a following vertical space. If "normal" text follows after \exercise
, this looks like it should. But if I add an enumeration after \exercise
, the vertical space looks too small---although it is the same. I don't know why, but maybe because of the space between the items (it's all about perception and a optical correction). That's why I want to add a very small additional space after the heading in \exercise
, if it's followed by an enumeration. So what I want to realize is something like "conditional spacing". Here's an example:
Code
\documentclass{scrartcl}
\usepackage[ngerman]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lipsum}
\usepackage{xparse}
\usepackage{noindentafter}
\DeclareDocumentCommand \exercise { } {%
\vspace{\baselineskip}
\noindent\textsf{\bfseries\Large Exercise}
\vspace{0.5\baselineskip}
\NoIndentAfterThis
}
\begin{document}
\exercise
\lipsum[1]
\begin{enumerate}
\item One
\item Two
\item Three
\end{enumerate}
\lipsum[1]
\exercise
\begin{enumerate}
\item One
\item Two
\item Three
\end{enumerate}
\end{document}
Document
Here's the result. In my opinion, the space after the second "Exercise" is a little bit too small. So I want to add some space after \exercise
, if it is followed by an enumeration.
\@ifnextchar
can be extended arbitrarily. Normally one would use e.g.\@ifnextchar[
for optionals. Why not use\@ifnextchar\foo
? (Untested, but I remember @egreg mentioned something like this.)\@ifnextchar
are dealing with\@ifnextchar[
. But is there a "modern" LaTeX3 equivalent to\@ifnextchar
? And what about testing if there is an environmentfoo
following?\peek_meaning(...):NTF
macros (page 59 of interface3.pdf).\exe
which typesets something like a headline. The normal usecase is to write normal text after\exe
. But sometimes you need an environmentgraphicscol
If (and only if) this environment follows after\exe
, there should be inserted some additional vertical space.