Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Suppose E1 is an expression in LaTeX, where one entry is x. Suppose E2 is another expression. How do you replace x by E2?

Can I wrap E2 in a box of certain size and have LaTeX replace x with that box without asking LaTeX to look inside the box and try to rearrange its internal structure?

For example: How do you replace an element of a given 3*3 matrix with a particular 2*2 matrix?

The answers so far are very interesting in themselves but none worked for me. So let me re-state. Suppose in an environment I can put a literal $x$ without any problem but I cannot put a matrix or a multi-line equation in the same spot, as it conflicts with various commands etc. How can I make a matrix act like an $x$ to the rest of program? As if I have to build the matrix somewhere else, put it in a box, and sell it to LateX as a single item as if it has no line return.

share|improve this question
    
Are you looking for command substitution or string substitution? –  krlmlr Feb 4 '12 at 8:04
    
@user946850, this is more like object substitution. But between command and string I would say it is closer to string. I am not sure if my tag or description is clear. See Werner's answer below where one object is substituted with another. I want to trick TeX into treating everything that appears between the equation delimiters as a single item, as if it was in a black box. So for example a matrix is to be treated as a single character. –  Maesumi Feb 4 '12 at 14:13
    
Perhaps a minipage environment? –  krlmlr Feb 4 '12 at 14:52

3 Answers 3

up vote 15 down vote accepted

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:

enter image description here

\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).

share|improve this answer

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.

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.