GeneralText result from a command

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
laurocesar
Posts: 7
Joined: Tue Dec 04, 2012 12:34 pm

Text result from a command

Post by laurocesar »

Hi guys!

I'm here again.

How can I compare the string (text) result from a command. For example, in MWE below, I expected that "It is equal".

Code: Select all

\documentclass{report}

\usepackage{ifthen}

\begin{document}

\newcommand{\one}{\textbf{LaTeX}\someothercommand}

\ifthenelse{\equalSUPER{\one}{LaTeX}}{It is equal}{It is not equal}

\end{document}
Thanks!
Last edited by cgnieder on Thu Dec 06, 2012 4:12 am, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Text result from a command

Post by cgnieder »

I am afraid you are out of luck here... If I understand you correctly you want to compare the text that is typeset in the end. For this one would need to expand the argument first before comparing strings.

In TeX what one can compare (basically) are tokens. One can compare if two tokens are equal in character code or equal in category code or both, and one can compare if the definitions of two control sequences have the same meaning. The last thing is what is usually done when one talks of “string” comparison in TeX. (This is by no means complete but should suffice here).

In the following example I'm using etoolbox but similar things are true for ifthen:

Code: Select all

\documentclass{article}
\usepackage{etoolbox}
\begin{document}
\def\test{test}
\ifstrequal{\test}{test}{TRUE}{FALSE}% FALSE
\expandafter\ifstrequal\expandafter{\test}{test}{TRUE}{FALSE}% TRUE
\end{document}
The above won't work with your example, though, as \expandafter only expands once which will only replace \textbf with \protect\textbf. One would need to expand the argument until only unexpandable material is left before comparing. However, [CTAN]\textbf[/CTAN] is not expandable. One could try something different, though:

Code: Select all

\documentclass{article}
\usepackage{etoolbox}
\makeatletter
\long\def\long@firstofone#1{#1}
\newcommand\compare[4]{%
  \begingroup
    \let\textbf\long@firstofone
    \long\edef\tmpa{#1}% expand everything and store in \tmpa
    \long\edef\tmpb{#2}% expand everything and store in \tmpb
    \ifdefequal\tmpa\tmpb{#3}{#4}%
  \endgroup
}
\makeatother
\begin{document}

\def\test{\textbf{test}}
\compare{\test}{test}{TRUE}{FALSE}% TRUE

\end{document}
This temporarily disables \textbf (similar things would be needed for \textit, ... ). As for \someothercommand: as long as we don't know how it is defined you might still be out of luck here.

Regards
site moderator & package author
laurocesar
Posts: 7
Joined: Tue Dec 04, 2012 12:34 pm

Re: Text result from a command

Post by laurocesar »

Thank you! That is it!
Post Reply