# Test if two things typeset identical results

I want to test if two somewhat arbitrary things typeset as exactly the same thing. For example I would like the test to tell me that abc is different from xyz, but is the same as \def\bar{abc}\bar. A poorly designed test and MWE is

\documentclass{article}
\makeatletter
\newsavebox{\@tempboxb}
\newcommand{\iftypesetsame}[4]{\begingroup%
\savebox{\@tempboxa}{#1}%
\savebox{\@tempboxb}{#2}%
\ifdim\ht\@tempboxa=\ht\@tempboxb%
\ifdim\dp\@tempboxa=\dp\@tempboxb%
\ifdim\wd\@tempboxa=\wd\@tempboxb%
#3%
\else%
#4%
\fi%
\else%
#4%
\fi%
\else%
#4%
\fi%
\endgroup}
\makeatother
\begin{document}
\iftypesetsame{abc}{abc}{same}{different}

\iftypesetsame{abc}{bcd}{same}{different}

\newcommand{\foo}{\begingroup\def\bar{abc}\bar\endgroup}
\iftypesetsame{abc}{\foo}{same}{different}

\iftypesetsame{xyz}{zyx}{same}{different} % Test gets it wrong

\iftypesetsame{a\vspace{1in}\par b}{ab}{same}{different} % Test gets it wrong
\end{document}


which tests based on the height, width and depth of the box. This is obviously not very robust because two very different things (e.g., xyz and zyx) can have the same width, height, and depth. It also doesn't handle arbitrary inputs (e.g., \par and \vspace}.

While I would like a perfect LaTeX solution, I would be interested in tests that fail less frequently of even LuaTeX based solutions

• The input processor doesn't have access to the typesetting engine: when a box has been packed you can look at its dimensions, but not at what is inside it. For instance, if you do \setbox0=\hbox{a} you can't check whether the box contains an a. You probably need LuaTeX, which has access to the nodes in a typeset box. – egreg Nov 21 '13 at 10:49
• @egreg so maybe the test cannot be perfect, but there must be tests that don't fail as often as my test. – StrongBad Nov 21 '13 at 11:04