1

I'd like to modify a piece of code, namely Friedhelm Sowa's picinpar.sty.

There is the command

\figwindow[#1,#2,#3,#4]

which inserts a picture (#3) #1 lines below the start of a paragraph at position #2 with caption #4

In the picinpar.sty file this command is defined as

 \long\def\figwindow[#1,#2,#3,#4] {%
   \advance\c@figure -1
   \begin{window}[#1,#2,{#3},{\def\@captype{figure}%
    \wincaption#4\par}] %
  }

I do not explore here further, what the window-environment is about, it is irrelevant for my question. What I'd like to achieve is an option that no caption is printed. That works well, if I rewrite figwindow as

 \long\def\figwindow[#1,#2,#3,#4] {%
  \advance\c@figure -1
  \begin{window}[#1,#2,{#3},{}]%
 }

My idea was now to use a conditional that prints no caption if #4 is {} and prints a caption otherwise. My attempt was:

 \long\def\figwindow[#1,#2,#3,#4] {%
  \advance\c@figure -1
  \begin{window}[#1,#2,{#3},\if #4{} {} \else   {\def\@captype{figure}%
    \wincaption#4\par}\fi]
}

Which unfortunately does not work. It ends up in the else-branch, if I call

\figwindow[#1,#2,#3,{}]

or \figwindow[#1,#2,#3,]

However modifying the style to comparing to a '*' instead to an empty token works perfectly:

\long\def\figwindow[#1,#2,#3,#4] {%
 \advance\c@figure -1
 \begin{window}[#1,#2,{#3},\if #4* {} \else {\def\@captype{figure}%
    \wincaption#4\par}\fi]
 }

\figwindow[#1,#2,#3,*]

Although I have a working solution, I'd prefer to make the {} one work (it appears more intuitive to me Surprisingly

\newcommand{\Compare}[2]{#1  \if #2{} EMPTY \else --- #2 ---\fi\par}
\Compare{EMPTY}{}
\Compare{FILLED}{HOW?}

works as I'd expect its output is

EMPTY - EMPTY -

FILLED - HOW? -

Any suggestions? Thank you in advance right now

GIC
  • 11
  • your working solution fails if #4 starts with eg xx. just use an empty argument not {} then \ifx\relax#4\relax – David Carlisle Sep 11 '22 at 07:45
  • For better stability than what @DavidCarlisle suggests, you can also use \if\relax\detokenize{#4}\relax (now only a really empty argument results in true). – Skillmon Sep 11 '22 at 07:55
  • For some more information on robustness and performance of ifempty-tests, this might be an interesting read (including the comments): https://tex.stackexchange.com/q/513337/117050 – Skillmon Sep 11 '22 at 13:28

1 Answers1

3

\if #2{}\else ...#2...\fi

expands #2 and compares the first two non expandable tokens so

if #2 is aabbcc the true branch is taken and it expands to bbcc{}

if #2 is a it compares a to { and takes the false branch, skipping over }

If #2 is empty it compares { to } which is false

You can test for #2 being empty with

\if\relax\detokenize{#2}\relax\else ...#2...\fi

as \detokenize never returns a command token that would test true with \relax (all non expandable command tokens test equal with \if) but if #2 is empty this is \if\relax\relax which is true.

David Carlisle
  • 757,742