TransWikia.com

if-condition for next command

TeX - LaTeX Asked by Titanlord on March 8, 2021

After I started building my own environments, I was wondering, if there is a if-condition, that checks what the next command is, and then gives the option to run something before the next command. A Pseudocode example for what I’m searching for:

newenvironment{test}{
    ifnext{begin{itemize}}{true: do something}{false: do other things}
    }
    {
}

2 Answers

LaTeX has the macro @ifnextchar. The name might be a bit misleading, you can not only use it to test the next single char, but also for a macro with a defined meaning, e.g., you can test whether the next thing in the input stream is begin.

This can be used to build a macro ifnextenv. It will take three arguments, the name of the environment, code which will be executed if that environment is following it, and code which is executed else. The macro will check whether the next thing in the input stream (after its three arguments) is begin. If that's the case it will check whether the first argument to begin matches the first argument to ifnextenv using pdf@strcmp.

Then it'll insert the branches according to the results of the test.

This usage example uses ifnextenv to automatically insert an itemize if after begin{test} no begin{enumerate} follows.

documentclass[]{article}

usepackage{pdftexcmds}

makeatletter
newcommandifnextenv[3]
  {%
    @ifnextcharbegin{@ifnextenv{#1}{#2}{#3}}{#3}%
  }
newcommand@ifnextenv[5]
  {%
    ifnumpdf@strcmp{#1}{#5}=0
      expandafter@firstoftwo
    else
      expandafter@secondoftwo
    fi
    {#2}
    {#3}%
    begin{#5}%
  }
makeatother

newenvironment{test}
  {%
    ifnextenv{enumerate}
      {deftest@end{}}
      {deftest@end{enditemize}itemize}%
  }
  {%
    test@end
  }

begin{document}
begin{test}
item foo
item bar
end{test}

begin{test}
  begin{enumerate}
    item foo
    item bar
  end{enumerate}
end{test}
end{document}

enter image description here

Correct answer by Skillmon on March 8, 2021

Here is a way to do it with a token-cycle.

Every time I find a begin{itemize}, I pre-execute PREitemize, which here I set to par Hi Mom, I am about to itemize. If there is a begin detected but it is not itemize, I pre-execute PREbegin, here set to par Sorry, this begin is not itemize.

documentclass{article}
usepackage{tokcycle}
defITM{itemize}
defTRUESET{T}
defPREitemize{par Hi Mom, I am about to itemize}
defPREbegin{par Sorry, this begin is not itemize}
stripgroupingtrue
tokcycleenvironmentfinditemize
{addcytoks{##1}}
{tctestifx{TRUESETBEGgo}%
  {deftmp{##1}tctestifx{ITMtmp}%
    {addcytoks[1]{PREitemize}}{addcytoks[1]{PREbegin}}
    addcytoks{begin{##1}}}%
  {addcytoks{{##1}}}%
 defBEGgo{F}%
}
{tctestifx{begin##1}{defBEGgo{T}}{addcytoks{##1}}}
{addcytoks{##1}}
begin{document}
finditemize
Here is a test
begin{itemize}
item A
begin{itemize}
item A.1
item A.2
end{itemize}
item B
item C
end{itemize}
begin{enumerate}
item A
item B
item C
end{enumerate}
endfinditemize
end{document}

enter image description here

Answered by Steven B. Segletes on March 8, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP