0

Is there a way to write functions on latex? For example...

def some_function{input}:
\command{input}

I've been thinking of using newcommand but I want to be able to use a variable for the input so that its not always the same characters.

1
  • 4
    \newcommand is the answer but it isn't clear what your question is? you can use \command{\myvariable} where \myvariable has also been defined with \newcommand to be input or whatever input you want to pass to \command Jul 17 at 20:21
1

Probably the following examples gives a clue of TeX's concept of replacing tokens by other tokens in the stage of expansion:

Example 1:

\documentclass{article}

\begin{document}


\verb|\newcommand\Command[1]{%|\\
\null\kern\parindent\verb|  Command got the following argument: \texttt{\detokenize{#1}}%|\\
\null\kern\parindent\verb|}|

\newcommand\Command[1]{%
  Command got the following argument: \texttt{\detokenize{#1}}%
}

\hrulefill

\verb|\Command{whatsoever tokens}|

yields:

\Command{whatsoever tokens}

\hrulefill

\verb|\newcommand*\Variable{initial value}|
\newcommand*\Variable{initial value}

\hrulefill

\verb|\Command{\Variable}|

yields:

\Command{\Variable}

\hrulefill

\verb|\expandafter\Command\expandafter{\Variable}|

yields:

\expandafter\Command\expandafter{\Variable}

\hrulefill

\verb|\renewcommand*\Variable{changed value}|
\renewcommand*\Variable{changed value}

\hrulefill

\verb|\Command{\Variable}|

yields:

\Command{\Variable}

\hrulefill

\verb|\expandafter\Command\expandafter{\Variable}|

yields:

\expandafter\Command\expandafter{\Variable}

\end{document}

enter image description here

Example 2:

\documentclass{article}

\newcommand\CommandA[1]{%  [1] -> You have "variable" #1. #1 is a placeholder for a set of tokens
  \par\noindent         %         that (without evaluation/expansion!) get placed instead of #1
  The following tokens were passed to \texttt{\string\CommandA} as argument:\\
  \texttt{\detokenize{#1}}%
}
\newcommand\CommandB[1]{%  [1] -> You have "variable" #1. #1 is a placeholder for a set of tokens
  \par\noindent         %         that (without evaluation/expansion!) get placed instead of #1
  The following tokens were passed to \texttt{\string\CommandB} as argument:\\
  \texttt{\detokenize{#1}}%
}%
\newcommand\MyFunction[2]{%  [2] -> You have "variables" #1 and #2. #1 and #2 are placeholders for
  \CommandA{#1}%                    sets of tokens that (without evaluation/expansion!) get placed
  \CommandB{#2}%                    instead of #1 respective #2
}%
% Besides macro-arguments you can as well use macros as "variables":
\newcommand*\VariableOne{VariableOne's initial value}
\newcommand*\VariableTwo{VariableTwo's initial value}

\pagestyle{plain}
\addtolength\topmargin{-2cm}
\addtolength\textheight{4cm}
\begin{document}

\begin{verbatim}
\newcommand\CommandA[1]{% [1] -> you have macro-argument/"variable" #1.
  \par\noindent         %       
  The following tokens were passed to \texttt{\string\CommandA} as argument:\\
  \texttt{\detokenize{#1}}%
}
\newcommand\CommandB[1]{% [1] -> you have macro-argument/"variable" #1
  \par\noindent
  The following tokens were passed to \texttt{\string\CommandB} as argument:\\
  \texttt{\detokenize{#1}}%
}%
\newcommand\MyFunction[2]{% [2] -> you have macro-arguments/"variables" #1 and #2
  \CommandA{#1}%
  \CommandB{#2}%
}%
% Besides macro-arguments you can as well use macros as "variables":
\newcommand*\VariableOne{VariableOne's initial value}
\newcommand*\VariableTwo{VariableTwo's initial value}
\end{verbatim}
\noindent\hrulefill

\noindent\hrulefill

\noindent\verb|\MyFunction{A}{B}|\\\null\kern2\parindent\emph{yields:} \MyFunction{A}{B}

\noindent\hrulefill

\noindent\verb|\MyFunction{X}{Y}|\\\null\kern2\parindent\emph{yields:} \MyFunction{X}{Y}

\noindent\hrulefill

\noindent\verb|\MyFunction{\VariableOne}{\VariableTwo}|\\\null\kern2\parindent\emph{yields:}
\MyFunction{\VariableOne}{\VariableTwo}

\noindent\hrulefill

\noindent\verb|\expandafter\MyFunction\expandafter{\VariableOne}{\VariableTwo}|\\\null\kern2\parindent\emph{yields:}
\expandafter\MyFunction\expandafter{\VariableOne}{\VariableTwo}

\noindent\hrulefill

\noindent\verb|\expandafter\MyFunction|\\
\verb|\expandafter{%|\\
\verb|\expandafter\VariableOne|\\
\verb|\expandafter}%|\\
\verb|\expandafter{%|\\
\verb|\VariableTwo}|\\
\null\kern2\parindent\emph{yields:}
\expandafter\MyFunction
\expandafter{%
\expandafter\VariableOne
\expandafter}%
\expandafter{%
\VariableTwo}

\noindent\hrulefill

\noindent\verb|\expandafter\expandafter\expandafter\MyFunction|\\
\verb|\expandafter\expandafter\expandafter{%|\\
\verb|\expandafter\VariableOne\expandafter}%|\\
\verb|\expandafter{\VariableTwo}|\\
\null\kern2\parindent\emph{yields:}
\expandafter\expandafter\expandafter\MyFunction
\expandafter\expandafter\expandafter{%
\expandafter\VariableOne\expandafter}%
\expandafter{\VariableTwo}

\noindent\hrulefill

\noindent\hrulefill

\noindent\verb|\renewcommand*\VariableOne{VariableOne's changed value}|\\
\verb|\renewcommand*\VariableTwo{VariableTwo's changed value}|
\renewcommand*\VariableOne{VariableOne's changed value}
\renewcommand*\VariableTwo{VariableTwo's changed value}

\noindent\hrulefill

\noindent\hrulefill

\noindent\verb|\MyFunction{\VariableOne}{\VariableTwo}|\\\null\kern2\parindent\emph{yields:}
\MyFunction{\VariableOne}{\VariableTwo}

\noindent\hrulefill

\noindent\verb|\expandafter\MyFunction\expandafter{\VariableOne}{\VariableTwo}|\\\null\kern2\parindent\emph{yields:}
\expandafter\MyFunction\expandafter{\VariableOne}{\VariableTwo}

\noindent\hrulefill

\noindent\verb|\expandafter\MyFunction|\\
\verb|\expandafter{%|\\
\verb|\expandafter\VariableOne|\\
\verb|\expandafter}%|\\
\verb|\expandafter{%|\\
\verb|\VariableTwo}|\\
\null\kern2\parindent\emph{yields:}
\expandafter\MyFunction
\expandafter{%
\expandafter\VariableOne
\expandafter}%
\expandafter{%
\VariableTwo}

\noindent\hrulefill

\noindent\verb|\expandafter\expandafter\expandafter\MyFunction|\\
\verb|\expandafter\expandafter\expandafter{%|\\
\verb|\expandafter\VariableOne\expandafter}%|\\
\verb|\expandafter{\VariableTwo}|\\
\null\kern2\parindent\emph{yields:}
\expandafter\expandafter\expandafter\MyFunction
\expandafter\expandafter\expandafter{%
\expandafter\VariableOne\expandafter}%
\expandafter{\VariableTwo}


\end{document}

enter image description here enter image description here

1

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.