2

I want to define a command \mycommand so that \mycommand 1 and \mycommand1 work differently. Here is my try based on \futurelet and \@sptoken, but it doesn't work:

\documentclass{article}
\makeatletter
\def\mycommand{\futurelet\@nexttoken\mycommand@}
\def\mycommand@{
    \ifx\@nexttoken\@sptoken
        space
    \else
        nospace
    \fi
}%
\makeatother
\begin{document}
\mycommand 1 
\mycommand1
\end{document}

Both of them give nospace 1

  • 3
    By the time \mycommand executes, the space is already gone. You could define \csname mycommand1\endcsname as a separate macro, but you would need to add \csname every time to execute it. – John Kormylo Jan 12 '24 at 02:17
  • 1
    Are you able and willing to use LuaLaTeX to compile your document. If so, you could employ LuaTeX's process_input_buffer callback and set up a Lua function that operates like a preprocessor on the input stream. The Lua function would use Lua's pattern matching to distinguish between \mycommand 1 and \mycommand1 and issue some replacement code based on which case holds. Once the preprocessor is done with its work, TeX will actually not "see" either \mycommand1 or \mycommand 1; instead, it will "see" whatever replacement code the Lua function was told to insert. – Mico Jan 12 '24 at 03:03
  • 1
    Could you explain why you want to do this? If people know the use-case, it is easier for them to suggest alternatives, which matters if you're asking for something impractical. I'm assuming you don't want, for example, to change the effect of spaces generally. – cfr Jan 12 '24 at 03:33
  • I'd like to define a command \myspacing that automatically adds something like \vspace*{1in}. I want some variants of this command \myspacing2, \myspacing3, \myspacing-1, ... that insert spaces of multiples of 1in. – user182849 Jan 12 '24 at 03:37
  • 1
    Already answered here https://tex.stackexchange.com/a/472250/250119 , but must appear in top level – user202729 Jan 12 '24 at 04:04
  • 2
    There's also https://tex.stackexchange.com/q/9718/250119 . That having said, it seems to me rather likely that any answer will be highly complicated that you wouldn't be able to modify the answer to your specific need, so you might as well stick to a more clumsy syntax of using optional argument (so \myspacing versus \myspacing[1]) – user202729 Jan 12 '24 at 04:06
  • 1
    Do yourself a favor and accept the fact that a) spaces after macros are really invisible to TeX, and b) numbers can not be used in macro names. Yes, you can invent complex things to avoid that and hack this around, but it will bite you later... \myspacing3 is not so better than \myspacing[3], and the latter one will be easy to do, and understood by any LaTeX user down the line. The first syntax is calling for troubles. – Rmano Jan 12 '24 at 08:49
  • @user182849 "I'd like to define a command \myspacing that automatically adds something like \vspace*{1in}." - How is this related to detecting a space character in the .tex-input file trailing the characters that get tokenized as the control word token \mycommand? – Ulrich Diez Jan 13 '24 at 21:05
  • After creating a control word token, e.g., \mycommand, or an ecplicit space token by reading/tokenizing some characters from a line of .tex-input and appending that token to the token-stream, the reading-apparatus of TeX switches to state S(skipping blanks) which implies that subsequent characters of the line of .tex-input whose current category code is 10(space) just get discarded instead of triggering the appending of whatsoever token to the token stream. – Ulrich Diez Jan 13 '24 at 21:17
  • So at the level, where macros are expanded - macros act on tokens, not on characters of the .tex-input-file, the space characters in question are already gone. – Ulrich Diez Jan 13 '24 at 21:17

0 Answers0