LaTeX forum ⇒ Math & ScienceCan you create functions / equations as something like an object?

Information and discussion about LaTeX's math and science related features (e.g. formulas, graphs).
carlklein
Posts: 1
Joined: Wed Nov 23, 2022 7:01 pm

Can you create functions / equations as something like an object?

Postby carlklein » Wed Nov 23, 2022 7:41 pm

Hi,

I'm attempting to compile a separate file with callable equations which feature the ability to be portions of equations, almost acting like an object.

For example, in an external file (call it "equations.tex"), I want to create generic mxn matrices A and B:

\begin{equation}\label{generic_A}
	\begin{pmatrix}
		a_{1, 1} &\; .\; .\; . & a_{1, n} \\
		.        &\; .\; .\; . & .        \\
		.        &\; .\; .\; . & .        \\
		.        & \; .\; .\; . & .        \\
		a_{m, 1} & \; .\; .\; . & a_{m, n}
	\end{pmatrix}
\end{equation}

\begin{equation}
	\begin{pmatrix}\label{generic_B}
		b_{1, 1} &\; .\; .\; . & b_{1, n} \\
		.        &\; .\; .\; . & .        \\
		.        &\; .\; .\; . & .        \\
		.        & \; .\; .\; . & .        \\
		b_{m, 1} & \; .\; .\; . & b_{m, n}
	\end{pmatrix}
\end{equation}


I need the following functionality:
1) To call a single matrix in another file
2) To combine matrices in equations in another file

How this would look is:

1) Every time I want to create a generic matrix, I don't want to have to type in the full matrix equation. It should look something along the lines of:

\call{generic_A}


2) I also need to be able to combine these "objects" in equations:
\begin{equation}
\call{generic_A} + \call{generic_B}
\end{equation}
\call{generic_A}


That was just an example, but I'd like this to extend this to other equations and functions.

There appears to be several different workarounds posted on coding forums, but they seem convoluted to me. I'm somewhat new to LaTeX, but I feel like there has to be a simple solution to this.

Thoughts? Recommendations?

Thank you!

Recommended reading 2021:

LaTeXguide.org • LaTeX-Cookbook.net
LaTeX Beginner's Guide LaTeX Cookbook
User avatar
MjK
Posts: 67
Joined: Fri Jan 28, 2022 6:09 pm

Can you create functions / equations as something like an object?

Postby MjK » Thu Nov 24, 2022 9:41 am

You can always put the content of an equation into an external file and load it using \input:
% Note: In real live the content of the following filecontents environment
%       would be an external file. Here, I'm  using filecontents to make
%       it work in the online editor/compiler.
\begin{filecontents}{generic_A.tex}
\begin{bNiceMatrix}
  a_{1,1} & \Cdots & a_{1,n} \\
  \Vdots  & \Ddots & \Vdots  \\
  a_{m,1} & \Cdots & a_{m,n} \\
\end{bNiceMatrix}%
\end{filecontents}
% Note: In real live the content of the following filecontents environment
%       would be an external file. Here, I'm  using filecontents to make
%       it work in the online editor/compiler.
\begin{filecontents}{generic_B.tex}
\begin{bNiceMatrix}
  b_{1,1} & \Cdots & b_{1,n} \\
  \Vdots  & \Ddots & \Vdots  \\
  b_{m,1} & \Cdots & b_{m,n} \\
\end{bNiceMatrix}%
\end{filecontents}
\documentclass{article}
\usepackage{mathtools}
\usepackage{nicematrix}

\begin{document}
In \eqref{eq:generic_A} you can see the single $a$ matrix.
\begin{equation}
  \label{eq:generic_A}
  \input{generic_A.tex}
\end{equation}
and \eqref{eq:generic_B} shows the $b$ matrix only.
\begin{equation}
  \label{eq:generic_B}
  \input{generic_B.tex}
\end{equation}
But \eqref{eq:A+B} shows both, the $a$ plus $b$ matrix.
\begin{equation}\label{eq:A+B}
  \input{generic_A.tex} + \input{generic_B.tex}
\end{equation}
\end{document}

However, be careful not to add empty lines at the very beginning or ending of the external files, because these would be paragraphs and depending on the usage of the file could result in strange behavior.

Often it is easier and sufficient to define your own commands:
\documentclass{article}
\usepackage{mathtools}
\usepackage{nicematrix}
\newcommand*{\MatrixA}{%
  \begin{bNiceMatrix}
    a_{1,1} & \Cdots & a_{1,n} \\
    \Vdots  & \Ddots & \Vdots  \\
    a_{m,1} & \Cdots & a_{m,n} \\
  \end{bNiceMatrix}%
}
\newcommand*{\MatrixB}{%
  \begin{bNiceMatrix}
    b_{1,1} & \Cdots & b_{1,n} \\
    \Vdots  & \Ddots & \Vdots  \\
    b_{m,1} & \Cdots & b_{m,n} \\
  \end{bNiceMatrix}%
}

\begin{document}
In \eqref{eq:generic_A} you can see the single $a$ matrix.
\begin{equation}
  \label{eq:generic_A}
  \MatrixA
\end{equation}
and \eqref{eq:generic_B} shows the $b$ matrix only.
\begin{equation}
  \label{eq:generic_B}
  \MatrixB
\end{equation}
But \eqref{eq:A+B} shows both, the $a$ plus $b$ matrix.
\begin{equation}
  \label{eq:A+B}
  \MatrixA + \MatrixB
\end{equation}
\end{document}


However you can also swap those definitions to an external file, if you need them for several document:
% Note: In real live the content of the following filecontents environment
%       would be an external file. Here, I'm  using filecontents to make
%       it work in the online editor/compiler.
\begin{filecontents}{carlsequationshortcuts.sty}
\ProvidesPackage{carlsequationshortcuts}[2022/11/24 my equation shortcuts]
\ProcessOptions\relax
\RequirePackage{nicematrix}
\newcommand*{\MatrixA}{%
  \begin{bNiceMatrix}
    a_{1,1} & \Cdots & a_{1,n} \\
    \Vdots  & \Ddots & \Vdots  \\
    a_{m,1} & \Cdots & a_{m,n} \\
  \end{bNiceMatrix}%
}
\newcommand*{\MatrixB}{%
  \begin{bNiceMatrix}
    b_{1,1} & \Cdots & b_{1,n} \\
    \Vdots  & \Ddots & \Vdots  \\
    b_{m,1} & \Cdots & b_{m,n} \\
  \end{bNiceMatrix}%
}
\end{filecontents}

\documentclass{article}
\usepackage{mathtools}
\usepackage{carlsequationshortcuts}

\begin{document}
In \eqref{eq:generic_A} you can see the single $a$ matrix.
\begin{equation}
  \label{eq:generic_A}
  \MatrixA
\end{equation}
and \eqref{eq:generic_B} shows the $b$ matrix only.
\begin{equation}
  \label{eq:generic_B}
  \MatrixB
\end{equation}
But \eqref{eq:A+B} shows both, the $a$ plus $b$ matrix.
\begin{equation}
  \label{eq:A+B}
  \MatrixA + \MatrixB
\end{equation}
\end{document}

Now, you also know, how to write your own LaTeX package. :o
My main topics are KOMA-Script and other questions related to my packages. If I reply to any other topic or if you've not yet added a minimal working example, please do not expect any further response. And please don't forget to tag examples as code.


Return to “Math & Science”

Who is online

Users browsing this forum: No registered users and 6 guests