6

I'd like to create a command which is currently beyond my capabilities so I was wondering if anyone around here could help me out.

What I'm looking to do is define a new command similar to the following:

\cmd{reference001}{Title}{text A}{text B}{text C}

This command would then create a new command called \reference001 which would display:

enter image description here

However, I would also like the \cmd command to define other new commands that will display only the parts of the input. E.g., it should generate a \reference001title command that will display Title and a \reference001partB that will display text B, etc.

Importantly, I'd really like to have numbers in my command name (which is possible as presented in this answer). I've put together what I've tried so far below, but I'm having trouble getting rid of all errors and particularly, with getting numbers to work as part of the command names.

To accomplish this, I've also looked at defining a custom label/ref system (via this question) which would be a perfectly valid solution for my purposes too. E.g., the \cmd above would then create multiple labels which could be referenced as \ref{reference001}, \ref{reference001partA}, etc. However I couldn't get that to work either.


\documentclass{article}

\newcommand{\cmd}[5]{
    \expandafter\newcommand\csname #1\endcsname{
        \textbf{#2}

        \textbf{\textit{Part A:}} #3

        \textbf{\textit{Part B:}} #4

        \textbf{\textit{Part C:}} #5
    }
    \expandafter\newcommand\csname #1title\endcsname{#2}
    \expandafter\newcommand\csname #1partA\endcsname{#3}
    \expandafter\newcommand\csname #1partB\endcsname{#4}
    \expandafter\newcommand\csname #1partC\endcsname{#5}
}

\begin{document}

    \cmd{reference}{Title}{text A}{text B}{text C}

    \reference

    \referencepartA

    \referencepartB

    \referencepartC

\end{document}
  • Not the real error, but the first inner \newcommmand is a typo – user31729 Jun 6 '17 at 15:01
  • A command can have numbers in it, but it can not be invoked as \ref001. \documentclass[12pt]{article} \usepackage{etoolbox} \begin{document} \csdef{ref001}{this is a test} \csuse{ref001} \end{document} – Steven B. Segletes Jun 6 '17 at 15:02
  • @StevenB.Segletes Can you provide a full example that would work with my MWE? – zephyr Jun 6 '17 at 15:06
  • 1
    the restriction on numbers is not a restriction on \newcommand it is a restriction on the tex syntax with standard catcode. \reference123 is \reference followed by the three tokens 1, 2, 3. – David Carlisle Jun 6 '17 at 15:08
6

If you don't need line breaking on your text, this could be the core of the \cmd you might envision.

\documentclass[12pt]{article}
\usepackage{etoolbox}
\usepackage[usestackEOL]{stackengine}
\newcommand\cmd[5]{%
  \csdef{#1title}{#2}%
  \csdef{#1partA}{#3}%
  \csdef{#1partB}{#4}%
  \csdef{#1partC}{#5}%
  \csdef{#1}{%
    \Longunderstack[l]{
      \textbf{#2}\\
      \textbf{\textit{Part A}:} #3\\
      \textbf{\textit{Part B}:} #4\\
      \textbf{\textit{Part C}:} #5\strut
    }%
  }%
}
\cmd{reference001}{Title}{text A}{text B}{text C}
\begin{document}
Here is ``\csuse{reference001partB}'' and here is the full reference:

\csuse{reference001}
\end{document}

enter image description here

  • Is it possible to make it so that that \cmd does not generate the main text? I'd like to ideally be able to use \cmd in the preamble to define everything, and reference the various components in the document. – zephyr Jun 6 '17 at 15:17
  • @zephyr Sure. Just remove the final \csuse{#1} from the definition. I have edited to reflect that. – Steven B. Segletes Jun 6 '17 at 15:22
1

You can implement a macro \CallMacro that takes advantage of brace delimited arguments and does the \csname..\endcsname-job for you while flipping around macro arguments as needed - no fancy packages needed:

enter image description here

\documentclass{article}
%%----------------------------------------------------------------------
%% Put a control sequence token in place instead of the string denoting
%% its name:
%%......................................................................
%%  \CallMacro<emptiness or tokens other than braces>{<Name of Control Sequence>}
%%
%% yields:
%%
%%  <emptiness or tokens other than braces>\Controlsequence
%%
%% E.g.,
%%
%%   \CallMacro foo{bar} -> foo\bar
%%   \CallMacro{bar} -> \bar
%%   \CallMacro\newcommand*{wEirdName}[1]{Arg 1: (#1)}
%%       -> \newcommand*\wEirdName[1]{Arg 1: (#1)}
%%
\newcommand\CallMacroIII[2]{#2#1}%
\newcommand\CallMacro{}\long\def\CallMacro#1#{\romannumeral\CallMacroII{#1}}%
\newcommand\CallMacroII[2]{%
  \expandafter\CallMacroIII\expandafter{\csname#2\endcsname}{0 #1}%
}%
%
\newcommand\cmd[5]{%
  \CallMacro\newcommand{#1}{%
    \vtop{%
      \hbox{\noindent\textbf{#2}\strut}%
      \hbox{\textbf{\textit{Part A: }}#3\strut}%
      \hbox{\textbf{\textit{Part B: }}#4\strut}%
      \hbox{\textbf{\textit{Part C: }}#5\strut}%
    }%
  }%
  \CallMacro\newcommand{#1title}{#2}%
  \CallMacro\newcommand{#1partA}{#3}%
  \CallMacro\newcommand{#1partB}{#4}%
  \CallMacro\newcommand{#1partC}{#5}%                 
}%
\begin{document}
\cmd{reference001}{Title}{text A}{text B}{text C}

\verb|\CallMacro{reference001}|:
\CallMacro{reference001}

\hrulefill

\verb|\CallMacro{reference001title}|:
\CallMacro{reference001title}

\hrulefill

\verb|\CallMacro{reference001partA}|:
\CallMacro{reference001partA}

\hrulefill

\verb|\CallMacro{reference001partB}|:
\CallMacro{reference001partB}

\hrulefill

\verb|\CallMacro{reference001partC}|:
\CallMacro{reference001partC}

\end{document}    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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