Latex 上的函数

Latex 上的函数

有没有办法在 latex 上编写函数?例如...

def some_function{input}:
\command{input}

我一直在考虑使用 newcommand,但我希望能够使用变量进行输入,以便它不总是相同的字符。

答案1

下面的例子可能给你一些关于 TeX 在扩展阶段用其他标记替换标记的概念的线索:

示例 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}

在此处输入图片描述

示例 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}

在此处输入图片描述 在此处输入图片描述

相关内容