Macro that allows you to choose from 2 texts

Here's a version with both macro \Amacro or a counter named acntr. I personally prefer the counter version since it does not involve a redefinition of \Amacro if necessary.

The test for 1 or else is done with \ifnum...\fi

Here I assumed that any value other than 1 will branch to Text2.

\documentclass{article}

\newcommand{\Amacro}{1}

\newcommand{\TwoTexts}[2]{%
  \ifnum\Amacro=1\relax%
  #1%
  \else
  #2%
  \fi
}


\newcounter{acntr}
\newcommand{\TwoTextsImproved}[2]{%
 \ifnum\value{acntr}=1\relax
 #1%
 \else
 #2%
 \fi
}

\begin{document}

\setcounter{acntr}{1}

\TwoTexts{You should see this}{Nope, not this}

\renewcommand{\Amacro}{2}
\TwoTexts{You should see this}{Nope, this is it}



\TwoTextsImproved{You should see this}{Nope, not this}
\stepcounter{acntr}

\TwoTextsImproved{You should see this}{Nope, this is it}


\end{document}

enter image description here


In order to prevent problems with unbalanced \if....\else..\fi I'd do the trick in terms of \Firstoftwo/\Secondoftwo:

\documentclass{article}

\newcommand\Firstoftwo[2]{#1}%
\newcommand\Secondoftwo[2]{#2}%

\newcommand\Amacro{1}%
\newcommand\setAmacro[1]{\renewcommand\Amacro{#1}}%

\newcommand{\TwoTexts}{%
  \ifnum\Amacro>1 %
    \expandafter\Secondoftwo
  \else
    \expandafter\Firstoftwo
  \fi
}

\begin{document}

\setAmacro{-3}

\texttt{\string\Amacro} is \Amacro.\\
\TwoTexts{Text in case the value in \texttt{\string\Amacro} is smaller than 1 or equal to 1}%
         {Text in case the value in \texttt{\string\Amacro} is larger than 1}%

\setAmacro{0}

\texttt{\string\Amacro} is \Amacro.\\
\TwoTexts{Text in case the value in \texttt{\string\Amacro} is smaller than 1 or equal to 1}%
         {Text in case the value in \texttt{\string\Amacro} is larger than 1}%

\setAmacro{1}

\texttt{\string\Amacro} is \Amacro.\\
\TwoTexts{Text in case the value in \texttt{\string\Amacro} is smaller than 1 or equal to 1}%
         {Text in case the value in \texttt{\string\Amacro} is larger than 1}%

\setAmacro{2}

\texttt{\string\Amacro} is \Amacro.\\
\TwoTexts{Text in case the value in \texttt{\string\Amacro} is smaller than 1 or equal to 1}%
         {Text in case the value in \texttt{\string\Amacro} is larger than 1}%

\setAmacro{17}

\texttt{\string\Amacro} is \Amacro.\\
\TwoTexts{Text in case the value in \texttt{\string\Amacro} is smaller than 1 or equal to 1}%
         {Text in case the value in \texttt{\string\Amacro} is larger than 1}%

\end{document}

By the way: Some years ago I wrote a routine \UD@KeepKthOfLArguments which lets you choose an arbitrary undelimited argument from an arbitrary amount of undelimited arguments which—due to recursion—is not bound to the 9-argument-limit usually imposed on TeX' macro-programming.

You can use that routine for creating another routine \choosetext that chooses undelimited arguments holding text-phrases depending on the numerical-value held in some variable/placeholder/macro/\count-register/whatsoever.

\documentclass{article}
\makeatletter
%%=========================================================================
%% Paraphernalia:
%%    \UD@firstoftwo, \UD@secondoftwo, \UD@PassFirstToSecond
%%.........................................................................
\newcommand\UD@firstoftwo[2]{#1}%
\newcommand\UD@secondoftwo[2]{#2}%
\newcommand\UD@PassFirstToSecond[2]{#2{#1}}%
%%-------------------------------------------------------------------------
%% Check whether argument is empty:
%%.........................................................................
%% \UD@CheckWhetherNull{<Argument which is to be checked>}%
%%                     {<Tokens to be delivered in case that argument
%%                       which is to be checked is empty>}%
%%                     {<Tokens to be delivered in case that argument
%%                       which is to be checked is not empty>}%
%%
%% The gist of this macro comes from Robert R. Schneck's \ifempty-macro:
%% <https://groups.google.com/forum/#!original/comp.text.tex/kuOEIQIrElc/lUg37FmhA74J>
%%
%% A concern in his posting is that the argument is hit with \string
%% after some expansions which in edge cases might result in unbalancing
%% surrounding \if..\fi-constructs if the macro is used inside of such
%% \if..\fi-constructs.
%%
%% That challenging concern sickened me. ;-)
%%
%% Therefore I decided to implerment a variant where this cannot happen
%% as expansion is forced by \romannumeral:
%%
%% After the first expansion-step, \string is not applied yet.
%% After the second expansion-step, any possibly disturbing remainders
%% are already removed due to \romannumeral-expansion.
%%
%% No eTeX- or whatsoever extensions. No \if.. .Only \romannumeral,
%% digit 0, space token for terminating \romannumeral-expansion,
%% \string, \expandafter, \UD@firstoftwo, \UD@secondoftwo, {, }.
%%
%% May 20, 2016
%%
%% Ulrich Diez (e-mail: [email protected])
%%
\newcommand\UD@CheckWhetherNull[1]{%
  \romannumeral0\expandafter\UD@secondoftwo\string{\expandafter
  \UD@secondoftwo\expandafter{\expandafter{\string#1}\expandafter
  \UD@secondoftwo\string}\expandafter\UD@firstoftwo\expandafter{\expandafter
  \UD@secondoftwo\string}\expandafter\expandafter\UD@firstoftwo{ }{}%
  \UD@secondoftwo}{\expandafter\expandafter\UD@firstoftwo{ }{}\UD@firstoftwo}%
}%
%%-------------------------------------------------------------------------
%% Keep only the K-th of L consecutive undelimited arguments.
%%   ( IF K < 1 OR K > L just remove L consecutive undelimited arguments. )
%%.........................................................................
%% \UD@KeepKthOfLArguments{<integer number K>}%
%%                        {<integer number L>}%
%%                        {<Tokens to insert before K-th argument>}%
%%                        <L consecutive undelimited arguments>
%%
%% If K >= 1 and K <= L  yields:
%%       <Tokens to insert before K-th argument><K-th undelimited argument>
%% If K < 1 or K > L
%%       (-> there is no K-th argument in the set
%%           of <L consecutive undelimited arguments> )
%% yields:
%%       <Tokens to insert before K-th argument>
%%
%% Examples:
%%
%% X\UD@KeepKthOfLArguments{3}{7}{<Tokens to insert>}{A}{B}{C}{D}{E}{F}{G}X
%% yields: X<Tokens to insert>CX
%%
%% X\UD@KeepKthOfLArguments{5}{2}{<Tokens to insert>}{A}{B}X
%% yields X<Tokens to insert>X
%%
%% X\UD@KeepKthOfLArguments{0}{2}{<Tokens to insert>}{A}{B}X
%% yields X<Tokens to insert>X
%%
%% X\romannumeral0%
%%  \UD@KeepKthOfLArguments{3}{7}{ <Tokens to insert>}{A}{B}{C}{D}{E}{F}{G}X
%% yields: X\romannumeral0 <Tokens to insert>CX
%% yields: X<Tokens to insert>CX
%% In case of embedding the whole thing in other expansion-contexts,
%% you need to have "hit" \romannumeral by only one \expandafter-chain for
%% obtaining the result...
%%
%% January 17, 2005
%%
%% Ulrich Diez (e-mail: [email protected])
%%
\newcommand\UD@KeepKthOfLArguments[2]{%
  \expandafter\UD@KeepKthOfLArgumentsA
  \expandafter{\romannumeral\number\number#1 000\expandafter}%
  \expandafter{\romannumeral\number\number#2 000}%
}%
\newcommand\UD@KeepKthOfLArgumentsA[3]{%
  \UD@CheckWhetherNull{#1}{%
    \UD@RemoveNArguments{#2}{#3}{}%
  }{%
    \expandafter\UD@PassFirstToSecond
    \expandafter{%
    \UD@firstoftwo{}#1}{\UD@KeepKthOfLArgumentsB{#1}{#2}}{#2}{#3}%
  }%
}%
\newcommand\UD@KeepKthOfLArgumentsB[5]{%
  \UD@CheckWhetherNull{#1}{%
     \UD@RemoveNArguments{#3}{\UD@RemoveNArguments{#2}{#5}}{}%
  }{%
    \UD@CheckWhetherNull{#2}{%
      \UD@RemoveNArguments{#4}{#5}{}%
    }{%
      \expandafter\UD@PassFirstToSecond
      \expandafter{%
      \UD@firstoftwo{}#2}{\expandafter\UD@KeepKthOfLArgumentsB
                       \expandafter{%
                       \UD@firstoftwo{}#1}}{#3}{#4}{#5}%
    }%
  }%
}%
\newcommand\UD@RemoveNArguments[3]{%
  \UD@CheckWhetherNull{#1}{#2#3}{%
    \UD@firstoftwo{%
      \expandafter\UD@RemoveNArguments
      \expandafter{\UD@firstoftwo{}#1}{#2}{#3}%
    }%
  }%
}%
%%-------------------------------------------------------------------------
%% A demo-routine:
%%
%% \choosetext{<number>}%
%%            {<Text in case number < 1>}%
%%            {<Text in case number = 1>}%
%%            {<Text in case number = 2>}%
%%            {<Text in case number = 3>}%
%%            {<Text in case number = 4>}%
%%            {<Text in case number = 5>}%
%%            {<Text in case number = 6>}%
%%            {<Text in case number = 7>}%
%%            {<Text in case number = 8>}%
%%            {<Text in case number = 9>}%
%%            {<Text in case number = 10>}%
%%            {<Text in case number = 11>}%
%%            {<Text in case number = 12>}%
%%            {<Text in case number = 13>}%
%%            {<Text in case number = 14>}%
%%            {<Text in case number > 14>}%
%%
%% The <number>-argument can hold anything that _expands_ to the components
%% of a quantity that is subsumable under the term <number> which is
%% explained in Donald E. Knuth's The TeXbook, Chapter 24: Summary of
%% Vertical Mode.
%%
%% Therefore <number> can as well be a macro-token expanding to a digit-
%% sequence, or a \the-call to a \count-register.
%%
%% January 24, 2017
%%
%% Ulrich Diez (e-mail: [email protected])
%%
%%.........................................................................
\newcommand\choosetext[1]{%
  \romannumeral0%
  \UD@KeepKthOfLArguments{\ifcase\expandafter\UD@firstoftwo
                                 \expandafter{\number\ifnum#1<1 0\else#1\fi}{} %
                          1\or2\or3\or4\or5\or6\or7\or8\or
                          9\or10\or11\or12\or13\or14\or15\else16\fi}%
                         {16}%
                         { }%
}%
%%-------------------------------------------------------------------------
%% A "variable" \Amacro and the routine \setAmacro for setting it's value.
%%.........................................................................
\newcommand\Amacro{1}%
\newcommand\setAmacro[1]{\renewcommand\Amacro{#1}}%
\makeatother

\begin{document}

The \verb|<number>|-argument of \verb|\choosetext| can hold anything that
expands to the components of a quantity that is subsumable under the term
\verb|<number>| which is explained in Donald E. Knuth's The TeXbook,
Chapter 24: Summary of Vertical Mode.
\bigskip

Thus the \verb|<number>|-argument of \verb|\choosetext| can hold a sequence
of digits, e.g., the digit \verb|5|:
\bigskip

\choosetext{5}{Text in case number is smaller than 1}%
              {Text in case number is 1}%
              {Text in case number is 2}%
              {Text in case number is 3}%
              {Text in case number is 4}%
              {Text in case number is 5}%
              {Text in case number is 6}%
              {Text in case number is 7}%
              {Text in case number is 8}%
              {Text in case number is 9}%
              {Text in case number is 10}%
              {Text in case number is 11}%
              {Text in case number is 12}%
              {Text in case number is 13}%
              {Text in case number is 14}%
              {Text in case number is larger than 14}%
\bigskip

The \verb|<number>|-argument of \verb|\choosetext| can as well hold a
macro-token which serves as variable/placeholder for numerical values.

\bigskip

\setAmacro{17}%
\verb|\Amacro| expands to the number \Amacro:
\choosetext{\Amacro}{Text in case number is smaller than 1}%
                   {Text in case number is 1}%
                   {Text in case number is 2}%
                   {Text in case number is 3}%
                   {Text in case number is 4}%
                   {Text in case number is 5}%
                   {Text in case number is 6}%
                   {Text in case number is 7}%
                   {Text in case number is 8}%
                   {Text in case number is 9}%
                   {Text in case number is 10}%
                   {Text in case number is 11}%
                   {Text in case number is 12}%
                   {Text in case number is 13}%
                   {Text in case number is 14}%
                   {Text in case number is larger than 14}%

\hrulefill

\setAmacro{15}%
\verb|\Amacro| expands to the number \Amacro:
\choosetext{\Amacro}{Text in case number is smaller than 1}%
                   {Text in case number is 1}%
                   {Text in case number is 2}%
                   {Text in case number is 3}%
                   {Text in case number is 4}%
                   {Text in case number is 5}%
                   {Text in case number is 6}%
                   {Text in case number is 7}%
                   {Text in case number is 8}%
                   {Text in case number is 9}%
                   {Text in case number is 10}%
                   {Text in case number is 11}%
                   {Text in case number is 12}%
                   {Text in case number is 13}%
                   {Text in case number is 14}%
                   {Text in case number is larger than 14}%

\hrulefill

\setAmacro{14}%
\verb|\Amacro| expands to the number \Amacro:
\choosetext{\Amacro}{Text in case number is smaller than 1}%
                   {Text in case number is 1}%
                   {Text in case number is 2}%
                   {Text in case number is 3}%
                   {Text in case number is 4}%
                   {Text in case number is 5}%
                   {Text in case number is 6}%
                   {Text in case number is 7}%
                   {Text in case number is 8}%
                   {Text in case number is 9}%
                   {Text in case number is 10}%
                   {Text in case number is 11}%
                   {Text in case number is 12}%
                   {Text in case number is 13}%
                   {Text in case number is 14}%
                   {Text in case number is larger than 14}%

\hrulefill

\setAmacro{13}%
\verb|\Amacro| expands to the number \Amacro:
\choosetext{\Amacro}{Text in case number is smaller than 1}%
                   {Text in case number is 1}%
                   {Text in case number is 2}%
                   {Text in case number is 3}%
                   {Text in case number is 4}%
                   {Text in case number is 5}%
                   {Text in case number is 6}%
                   {Text in case number is 7}%
                   {Text in case number is 8}%
                   {Text in case number is 9}%
                   {Text in case number is 10}%
                   {Text in case number is 11}%
                   {Text in case number is 12}%
                   {Text in case number is 13}%
                   {Text in case number is 14}%
                   {Text in case number is larger than 14}%

\hrulefill

\setAmacro{12}%
\verb|\Amacro| expands to the number \Amacro:
\choosetext{\Amacro}{Text in case number is smaller than 1}%
                   {Text in case number is 1}%
                   {Text in case number is 2}%
                   {Text in case number is 3}%
                   {Text in case number is 4}%
                   {Text in case number is 5}%
                   {Text in case number is 6}%
                   {Text in case number is 7}%
                   {Text in case number is 8}%
                   {Text in case number is 9}%
                   {Text in case number is 10}%
                   {Text in case number is 11}%
                   {Text in case number is 12}%
                   {Text in case number is 13}%
                   {Text in case number is 14}%
                   {Text in case number is larger than 14}%

\hrulefill

\setAmacro{11}%
\verb|\Amacro| expands to the number \Amacro:
\choosetext{\Amacro}{Text in case number is smaller than 1}%
                   {Text in case number is 1}%
                   {Text in case number is 2}%
                   {Text in case number is 3}%
                   {Text in case number is 4}%
                   {Text in case number is 5}%
                   {Text in case number is 6}%
                   {Text in case number is 7}%
                   {Text in case number is 8}%
                   {Text in case number is 9}%
                   {Text in case number is 10}%
                   {Text in case number is 11}%
                   {Text in case number is 12}%
                   {Text in case number is 13}%
                   {Text in case number is 14}%
                   {Text in case number is larger than 14}%

\hrulefill

\setAmacro{10}%
\verb|\Amacro| expands to the number \Amacro:
\choosetext{\Amacro}{Text in case number is smaller than 1}%
                   {Text in case number is 1}%
                   {Text in case number is 2}%
                   {Text in case number is 3}%
                   {Text in case number is 4}%
                   {Text in case number is 5}%
                   {Text in case number is 6}%
                   {Text in case number is 7}%
                   {Text in case number is 8}%
                   {Text in case number is 9}%
                   {Text in case number is 10}%
                   {Text in case number is 11}%
                   {Text in case number is 12}%
                   {Text in case number is 13}%
                   {Text in case number is 14}%
                   {Text in case number is larger than 14}%

\hrulefill

\setAmacro{9}%
\verb|\Amacro| expands to the number \Amacro:
\choosetext{\Amacro}{Text in case number is smaller than 1}%
                   {Text in case number is 1}%
                   {Text in case number is 2}%
                   {Text in case number is 3}%
                   {Text in case number is 4}%
                   {Text in case number is 5}%
                   {Text in case number is 6}%
                   {Text in case number is 7}%
                   {Text in case number is 8}%
                   {Text in case number is 9}%
                   {Text in case number is 10}%
                   {Text in case number is 11}%
                   {Text in case number is 12}%
                   {Text in case number is 13}%
                   {Text in case number is 14}%
                   {Text in case number is larger than 14}%

\hrulefill

\setAmacro{8}%
\verb|\Amacro| expands to the number \Amacro:
\choosetext{\Amacro}{Text in case number is smaller than 1}%
                   {Text in case number is 1}%
                   {Text in case number is 2}%
                   {Text in case number is 3}%
                   {Text in case number is 4}%
                   {Text in case number is 5}%
                   {Text in case number is 6}%
                   {Text in case number is 7}%
                   {Text in case number is 8}%
                   {Text in case number is 9}%
                   {Text in case number is 10}%
                   {Text in case number is 11}%
                   {Text in case number is 12}%
                   {Text in case number is 13}%
                   {Text in case number is 14}%
                   {Text in case number is larger than 14}%

\hrulefill

\setAmacro{7}%
\verb|\Amacro| expands to the number \Amacro:
\choosetext{\Amacro}{Text in case number is smaller than 1}%
                   {Text in case number is 1}%
                   {Text in case number is 2}%
                   {Text in case number is 3}%
                   {Text in case number is 4}%
                   {Text in case number is 5}%
                   {Text in case number is 6}%
                   {Text in case number is 7}%
                   {Text in case number is 8}%
                   {Text in case number is 9}%
                   {Text in case number is 10}%
                   {Text in case number is 11}%
                   {Text in case number is 12}%
                   {Text in case number is 13}%
                   {Text in case number is 14}%
                   {Text in case number is larger than 14}%

\hrulefill

\setAmacro{6}%
\verb|\Amacro| expands to the number \Amacro:
\choosetext{\Amacro}{Text in case number is smaller than 1}%
                   {Text in case number is 1}%
                   {Text in case number is 2}%
                   {Text in case number is 3}%
                   {Text in case number is 4}%
                   {Text in case number is 5}%
                   {Text in case number is 6}%
                   {Text in case number is 7}%
                   {Text in case number is 8}%
                   {Text in case number is 9}%
                   {Text in case number is 10}%
                   {Text in case number is 11}%
                   {Text in case number is 12}%
                   {Text in case number is 13}%
                   {Text in case number is 14}%
                   {Text in case number is larger than 14}%

\hrulefill

\setAmacro{5}%
\verb|\Amacro| expands to the number \Amacro:
\choosetext{\Amacro}{Text in case number is smaller than 1}%
                   {Text in case number is 1}%
                   {Text in case number is 2}%
                   {Text in case number is 3}%
                   {Text in case number is 4}%
                   {Text in case number is 5}%
                   {Text in case number is 6}%
                   {Text in case number is 7}%
                   {Text in case number is 8}%
                   {Text in case number is 9}%
                   {Text in case number is 10}%
                   {Text in case number is 11}%
                   {Text in case number is 12}%
                   {Text in case number is 13}%
                   {Text in case number is 14}%
                   {Text in case number is larger than 14}%

\hrulefill

\setAmacro{4}%
\verb|\Amacro| expands to the number \Amacro:
\choosetext{\Amacro}{Text in case number is smaller than 1}%
                   {Text in case number is 1}%
                   {Text in case number is 2}%
                   {Text in case number is 3}%
                   {Text in case number is 4}%
                   {Text in case number is 5}%
                   {Text in case number is 6}%
                   {Text in case number is 7}%
                   {Text in case number is 8}%
                   {Text in case number is 9}%
                   {Text in case number is 10}%
                   {Text in case number is 11}%
                   {Text in case number is 12}%
                   {Text in case number is 13}%
                   {Text in case number is 14}%
                   {Text in case number is larger than 14}%

\hrulefill

\setAmacro{3}%
\verb|\Amacro| expands to the number \Amacro:
\choosetext{\Amacro}{Text in case number is smaller than 1}%
                   {Text in case number is 1}%
                   {Text in case number is 2}%
                   {Text in case number is 3}%
                   {Text in case number is 4}%
                   {Text in case number is 5}%
                   {Text in case number is 6}%
                   {Text in case number is 7}%
                   {Text in case number is 8}%
                   {Text in case number is 9}%
                   {Text in case number is 10}%
                   {Text in case number is 11}%
                   {Text in case number is 12}%
                   {Text in case number is 13}%
                   {Text in case number is 14}%
                   {Text in case number is larger than 14}%

\hrulefill

\setAmacro{2}%
\verb|\Amacro| expands to the number \Amacro:
\choosetext{\Amacro}{Text in case number is smaller than 1}%
                   {Text in case number is 1}%
                   {Text in case number is 2}%
                   {Text in case number is 3}%
                   {Text in case number is 4}%
                   {Text in case number is 5}%
                   {Text in case number is 6}%
                   {Text in case number is 7}%
                   {Text in case number is 8}%
                   {Text in case number is 9}%
                   {Text in case number is 10}%
                   {Text in case number is 11}%
                   {Text in case number is 12}%
                   {Text in case number is 13}%
                   {Text in case number is 14}%
                   {Text in case number is larger than 14}%

\hrulefill

\setAmacro{1}%
\verb|\Amacro| expands to the number \Amacro:
\choosetext{\Amacro}{Text in case number is smaller than 1}%
                   {Text in case number is 1}%
                   {Text in case number is 2}%
                   {Text in case number is 3}%
                   {Text in case number is 4}%
                   {Text in case number is 5}%
                   {Text in case number is 6}%
                   {Text in case number is 7}%
                   {Text in case number is 8}%
                   {Text in case number is 9}%
                   {Text in case number is 10}%
                   {Text in case number is 11}%
                   {Text in case number is 12}%
                   {Text in case number is 13}%
                   {Text in case number is 14}%
                   {Text in case number is larger than 14}%

\hrulefill

\setAmacro{0}%
\verb|\Amacro| expands to the number \Amacro:
\choosetext{\Amacro}{Text in case number is smaller than 1}%
                   {Text in case number is 1}%
                   {Text in case number is 2}%
                   {Text in case number is 3}%
                   {Text in case number is 4}%
                   {Text in case number is 5}%
                   {Text in case number is 6}%
                   {Text in case number is 7}%
                   {Text in case number is 8}%
                   {Text in case number is 9}%
                   {Text in case number is 10}%
                   {Text in case number is 11}%
                   {Text in case number is 12}%
                   {Text in case number is 13}%
                   {Text in case number is 14}%
                   {Text in case number is larger than 14}%

\hrulefill

\setAmacro{-1}%
\verb|\Amacro| expands to the number \Amacro:
\choosetext{\Amacro}{Text in case number is smaller than 1}%
                   {Text in case number is 1}%
                   {Text in case number is 2}%
                   {Text in case number is 3}%
                   {Text in case number is 4}%
                   {Text in case number is 5}%
                   {Text in case number is 6}%
                   {Text in case number is 7}%
                   {Text in case number is 8}%
                   {Text in case number is 9}%
                   {Text in case number is 10}%
                   {Text in case number is 11}%
                   {Text in case number is 12}%
                   {Text in case number is 13}%
                   {Text in case number is 14}%
                   {Text in case number is larger than 14}%

\end{document}

Tags:

Macros