How do you do substitutions in LaTeX?
Yes, this is possible, since (La)TeX is a macro-based language, and these macros can be modified. Take your second, more concrete construction as an example: "Replace an element of a given 3 x 3 matrix with a particular 2 x 2 matrix. The following example illustrates that:
\documentclass{article}
\usepackage{amsmath}% http://ctan.org/pkg/amsmath
\begin{document}
\newcommand{\x}{1}
\[
\begin{bmatrix}
3 & 2 & 8 \\
5 & \x & 6 \\
9 & 4 & 7
\end{bmatrix}
\]
\renewcommand{\x}{\begin{array}{@{}cc@{}}1 & 1 \\ 1 & 1\end{array}}
\[
\begin{bmatrix}
3 & 2 & 8 \\
5 & \x & 6 \\
9 & 4 & 7
\end{bmatrix}
\]
\end{document}
In the first 3 x 3 matrix, the element \x
is defined as 1
. The matrix is duplicated with the element \x
redefined as a 2 x 2 array. Of course, it would even be possible to define a macro that typesets the 3 x 3 matrix itself and allows for specifying an argument that prints \x
:
\newcommand{\mymatrix}[1]{%
\begin{bmatrix}
3 & 2 & 8 \\
5 & #1 & 6 \\
9 & 4 & 7
\end{bmatrix}
}
Boxing is another option for replacement of items. For example, using
\newsavebox{\mybox}% storage box
\savebox{\mybox}{\hbox{$begin{bmatrix}
3 & 2 & 8 \\
5 & 1 & 6 \\
9 & 4 & 7
\end{bmatrix}$}}
allows you to now do
\usebox{\mybox}
in a similar way to using macros. The difference between macros and boxing is that macros are typically expanded in context, while boxes are set in place (or fixed in terms of formatting at the time of definition).
When Knuth designed TeX, he provided the capability of defining macros, which are approximately equivalent to what a function or procedure is in other computer languages. In Plain TeX one can use \def
among other constructs or in LaTeX \newcommand
(there are many other variants, but we will omit them here for clarity).
LaTeX, also provides \newenvironment
, which is a very useful construct for defining environments. We will use this command to creat a more flexible approach to enable us to typeset, as many cells and as many rows one wants.
The input will be captured as,
\SetArray{llll}
\begin{Array}
\addrow{...}
...
\end{Array}
the complete MWE is shown below:
\documentclass{article}
\newcommand\addrow[1]{#1\\}
\newcommand\SetArray[1]{%
\newcommand\setarray{#1}
}
\newenvironment{Array}{%
\[ \left( \begin{array}{\setarray}}{
\end{array} \right)\]}
\begin{document}
\SetArray{llll}
\begin{Array}
\addrow{1&2&3&10}
\addrow{4&5&6&10}
\addrow{7&8&9&10}
\end{Array}
\end{document}
Using more flexible commands, one can typeset as many cells and as many rows required and the command names can have semantics that parallel the author's actions.
In addition to command substitution, if you really wanted to replace a string by another string, consider the fairly recent stringstrings
package:
\convertword{string}{ring}{rong}
would expand to strong
.