Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I have a command that receives some argument, like:

\newcommand{\myappend}[1]{
\ifthenelse{\equal{#1}{aaaBB}}{\dothis}{\dothat}
}

Assume that the argument is 'aaa'. How to append (inside the command) 'BB' to #1, that is, how to edit #1 ?

share|improve this question
    
You have your example set up as an \ifthenelse, but question posits no such branching, but merely asks how to append to an argument, such as \def\myappend#1{#1BB} –  Steven B. Segletes Mar 3 at 2:19
    
I read your question several times, but I can't understand what you're asking. Isn't \ifthenelse{\equal{#1BB}{aaaBB}} what you're looking for? –  egreg Mar 3 at 7:47

3 Answers 3

I am not sure I understood the question. Here are two options: the first one, using an auxiliary command to add "BB" to #1 and the second one does the same but using \StrSubstitute from the xstring package:

\documentclass{article}
\usepackage{ifthen}
\usepackage{xstring}

\def\dothis{this}
\def\dothat{that}

\newcommand\myappend[1]{%
  \def\x{#1BB}%
  \ifthenelse{\equal{\x}{aaaBB}}{\dothis}{\dothat}%
}

\newcommand\myappendi[1]{%
  \StrSubstitute{#1}{aaa}{aaaBB}[\x]%
  \ifthenelse{\equal{\x}{aaaBB}}{\dothis}{\dothat}%
}

\begin{document}

\myappend{aaa}\ \myappend{aaaBB}

\myappendi{aaa}\ \myappendi{aaaBB}

\end{document}

enter image description here

share|improve this answer

With \ifstrequal from etoolbox:

\documentclass{article}
\usepackage{etoolbox}
\newcommand{\myappend}[1]{%
\ifstrequal{#1}{aaaBB}{}{aaaBB}
}%
\begin{document}
  \myappend{aaa}
\end{document}

And

\documentclass{article}
\usepackage{ifthen}
\newcommand{\myappend}[1]{%
\ifthenelse{\equal{#1}{aaaBB}}{}{aaaBB}
}%
\begin{document}
  \myappend{aaa}
\end{document}
share|improve this answer

String comparison under pdfTeX is made easily possible via the primitive \pdfstrcmp:

enter image description here

\documentclass{article}
% \pdfstrcmp{<string1>}{<string2>}
% \pdfstrcmp compares two strings and expands to 0 if the strings are equal, to -1 if the first string
%   ranks before the second, and to 1 otherwise
\newcommand{\myappend}[1]{%
  \ifnum\pdfstrcmp{#1}{aaa}=0
    This% Strings are equal
  \else
    That% Strings are not equal
  \fi%
}
\begin{document}
\myappend{aab} % That
\myappend{aaA} % That
\myappend{aaa} % This
\myappend{AAa} % That

\newcommand{\aaa}{aaa}
\myappend{\aaa} % This
\end{document}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.