2

I'm trying to make definitions from definitions (I don't know if this is how it should be called), here is an example (updated to make clear of my actual aim):

\documentclass[a4paper,12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{ifthen}
\usepackage{lipsum}

\def\street{num. 1, my street} \def\block{my block} \def\town{my town, my zipcode} \def\state{my state}

\def\doordetails{% \ifthenelse{\equal{\street}{}}{}{\street}% \ifthenelse{\equal{\street}{} \or \equal{\street}{}}{}{, }% \ifthenelse{\equal{\block}{}}{}{\block}}

\def\regiondetails{% \ifthenelse{\equal{\town}{}}{}{\town}% \ifthenelse{\equal{\town}{} \or \equal{\state}{}}{}{, }% \ifthenelse{\equal{\state}{}}{}{\state}}

\begin{document} \ifthenelse{\equal{\doordetails}{}}{}{\doordetails} \ifthenelse{\equal{\doordetails}{} \or \equal{\regiondetails}{}}{}{~\textbullet~}% \ifthenelse{\equal{\regiondetails}{}}{}{\regiondetails}} \end{document}

But I get this error when trying building

! Missing \endcsname inserted.
<to be read again> 
\let 
l.15 \ifthenelse{\equal{\doordetails}{}}{xxx}{xxx}
                                    \\%

  • that won't work. \ifthenelse is not expandable. What are you really trying to achieve? – Ulrike Fischer Apr 11 '21 at 08:48
  • \ifthenelse tests cannot be nested; you can use \ifthenelse in the “true” and “false“ branches, but not inside the test. Maybe some more details about your real aim can help in finding a way out. – egreg Apr 11 '21 at 08:49
  • i updated, let me know if you need more info. – user1850133 Apr 11 '21 at 10:43
  • I don't think I would even use ifthen here. I'd use the tools from the etoolbox package, especially \ifdefvoid. I have a letter class where all the formatting is using this construction to see if a macro exists, is \relax or is "empty". – daleif Apr 11 '21 at 11:40

3 Answers3

1

You are using primitive \def, so you can use directly other related primitives and do not rely on LaTeX packages:

\def\street{num. 1, my street}
\def\block{my block}
\def\town{my town, my zipcode}
\def\state{my state}

\def\doordetails{% \street \ifx\relax\street\block\relax \else , \fi \block } \def\regiondetails{% \town \ifx\relax\town\state\relax \else , \fi \state }

\doordetails \ifx\relax\doordetails\regiondetails\relax\else ~$\bullet$~\fi \regiondetails

wipet
  • 74,238
0

I may not have interpreted the OP's logic properly with regard to the text bullet (when to appear and when not), but the point is you can use simple \if and \ifx to address nested conditions.

When macro is either blank or filled, you use \ifx\empty\<macro-name> and if the macro expands to something or nothing, then use \if\relax\<macro-name>\relax.

\documentclass[a4paper,12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{ifthen}
\usepackage{lipsum}

\def\street{num. 1, my street} \def\block{my block} \def\town{my town, my zipcode} \def\state{my state}

\def\doordetails{% \ifx\street\empty \block \else \street \ifx\block\empty \else , \block \fi \fi }

\def\regiondetails{% \ifx\town\empty \state \else \town \ifx\state\empty \else , \state \fi \fi }

\def\alldetails{ \if\relax\doordetails\relax \regiondetails \else \doordetails \if\relax\regiondetails\relax \else ~\textbullet~\regiondetails \fi \fi} \begin{document} \alldetails

\def\street{}\alldetails

\def\town{}\alldetails

\def\block{}\alldetails

\end{document}

enter image description here

0

You cannot nest \ifthenelse calls.

Here's a replacement, but note some differences: \or should be || and parentheses should be used straight, not like \( and \) as is the case with \ifthenelse. The code is from https://tex.stackexchange.com/a/546445/4427 with a few changes. These conditionals are fully expandable, so they can be nested.

\documentclass[a4paper,12pt]{article}

\ExplSyntaxOn \NewExpandableDocumentCommand{\xifthenelse}{mmm} { \bool_if:nTF { #1 } { #2 } { #3 } } \cs_new_eq:NN \numtest \int_compare_p:n \cs_new_eq:NN \oddtest \int_if_odd_p:n \cs_new_eq:NN \fptest \fp_compare_p:n \cs_new_eq:NN \dimtest \dim_compare_p:n \cs_new_eq:NN \deftest \cs_if_exist_p:N \cs_new_eq:NN \namedeftest \cs_if_exist_p:c \cs_new_eq:NN \eqdeftest \token_if_eq_meaning_p:NN \cs_new_eq:NN \streqtest \str_if_eq_p:ee \cs_new_eq:NN \emptytest \tl_if_blank_p:n \cs_new_eq:NN \booleantest \legacy_if_p:n \ExplSyntaxOff

\newcommand\doordetails{% \xifthenelse{\emptytest{\street}}{}{\street}% \xifthenelse{\emptytest{\street} || \emptytest{\block}}{}{, }% \xifthenelse{\emptytest{\block}}{}{\block}% }

\newcommand\regiondetails{% \xifthenelse{\emptytest{\town}}{}{\town}% \xifthenelse{\emptytest{\town} || \emptytest{\state}}{}{, }% \xifthenelse{\emptytest{\state}}{}{\state}% }

\begin{document}

\newcommand\street{num. 1, my street} \newcommand\block{my block} \newcommand\town{my town, my zipcode} \newcommand\state{my state}

\xifthenelse{\emptytest{\doordetails}}{}{\doordetails} \xifthenelse{\emptytest{\doordetails} || \emptytest{\regiondetails}}{}{~\textbullet~}% \xifthenelse{\emptytest{\regiondetails}}{}{\regiondetails}

\end{document}

enter image description here

egreg
  • 1,121,712