Generalexecuting commands by string

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
foobar
Posts: 4
Joined: Sat May 15, 2010 2:44 pm

executing commands by string

Post by foobar »

That is my code:

Code: Select all

\def\Name{Patrick}
I now want to execute the command "Name" not by writing \Name, but by using a function such like:

Code: Select all

\execute{Name}
Does anyone know such a function in (La)TeX?

I know I could just use any Scripting Language.
I know I could just write a backslash and the command name to a file and read it back in.
But I want a clean solution using just (La)TeX.

Can anybody help me?

Recommended reading 2024:

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

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

frabjous
Posts: 2064
Joined: Fri Mar 06, 2009 12:20 am

executing commands by string

Post by frabjous »

You could use the ifthen package as so:

Code: Select all

\documentclass{article}
\usepackage{ifthen}

\newcommand{\execute}[1]{%
\ifthenelse{\equal{#1}{Name}}{%
      Do this for ``Name''
}{%
      Do this for other executions.
}}
\begin{document}

\execute{Name}

\execute{Somethingelse}

\end{document}
See the documentation for more info.

I really don't see the point, though.
josephwright
Site Moderator
Posts: 814
Joined: Tue Jul 01, 2008 2:19 pm

executing commands by string

Post by josephwright »

Isn't this just

Code: Select all

\csname Name\endcsname
which is wrapped up by the LaTeX kernel as a macro, which could be given a user space name

Code: Select all

\newcommand*\csuse[1]{\csname#1\endcsname}
(This function is provided by the etoolbox package, by the way.)
Joseph Wright
foobar
Posts: 4
Joined: Sat May 15, 2010 2:44 pm

Re: executing commands by string

Post by foobar »

I was looking for the second solution.

Thanks Joseph!
Post Reply