This is the best I can think: use a tikz matrix to create a matrix of math nodes
(which you can include inside a math environment and delimit with brackets if you want), and then use the implicit naming of nodes to refer to individual cells of the matrix, as for example: m-1-1.north east
to refer to the north east corner of the first element.
In order to avoid alignment problems, you have to ensure that all the nodes of that matrix have the same dimensions, by giving a minimum width
and minimum height
option. I'm not very satisfied with this solution, because it requires you to know the dimensions of the larger cell. However, appropiate values are not difficult to find by trial and error.
After some tries, my code is the following:
\documentclass{article}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{inputenc}
\usepackage{xcolor}
\usepackage{tikz}
\begin{document}
\thispagestyle{empty}
\usetikzlibrary{matrix}
\usetikzlibrary{calc,fit}
\tikzset{%
highlight1/.style={rectangle,rounded corners,color=red!,fill=red!15,draw,fill opacity=0.5,thick,inner sep=0pt}
}
\tikzset{%
highlight2/.style={rectangle,rounded corners,color=green!,fill=green!15,draw,fill opacity=0.5,thick,inner sep=0pt}
}
\begin{equation}
\renewcommand{\arraystretch}{1.5}
A_{L}=
\begin{tikzpicture}[baseline=(m.center)]
\matrix (m) [matrix of math nodes, left delimiter={[}, right delimiter={]},
row sep=1mm, nodes={minimum width=3em, minimum height=1.6em}] {
-T^{1}_{11} & 0 & 0 & -T^{1}_{12} \\
-T^{2}_{12} & -T^{2}_{11} & 0 & 0 \\
0 & -T^{3}_{12} & |(r)| T^{3}_{11} & 0 \\
0 & 0 & 0 & 0 \\
};
\node[highlight2, fit=(m-1-1.north west) (m-2-2.south east)] {};
\node[highlight1, fit=(m-3-1.north west) (m-4-4.south east)] {};
\end{tikzpicture}
\left[\begin{array}{c}
\phi_{A} \\
\phi_{B} \\
\phi_{C} \\
\phi_{D}
\end{array}\right]
\label{eq:ALphif}
\end{equation}
\begin{equation}\renewcommand{\arraystretch}{1.5}
B_{L}=
\begin{tikzpicture}[baseline=(m.center)]
\matrix (m) [matrix of math nodes, left delimiter={[}, right delimiter={]},
row sep=1mm, nodes={minimum width=5.5em, minimum height=1.6em}] {
(T^{1}_{11}+T^{1}_{12}) & 0 & 0 \\
0 & (T^{2}_{11}+T^{2}_{12}) & 0 \\
0 & 0 & (T^{3}_{11}+T^{3}_{12}) \\
0 & 0 & 0 \\
};
\node[highlight2, fit=(m-1-1.north west) (m-2-2.south east)] {};
\node[highlight1, fit=(m-3-1.north west) (m-4-3.south east)] {};
\end{tikzpicture}
\left[\begin{array}{c}
\phi_{1} \\
\phi_{2} \\
\phi_{3}
\end{array}\right]
\label{eq:BLphii}
\end{equation}
\begin{equation}
\renewcommand{\arraystretch}{1.5}
A_{R}=
\begin{tikzpicture}[baseline=(m.center)]
\matrix (m) [matrix of math nodes, left delimiter={[}, right delimiter={]},
row sep=1mm, nodes={minimum width=2.5em, minimum height=1.6em}] {
T^{2}_{22} & T^{2}_{21} & 0 & 0 \\
0 & T^{3}_{22} & T^{3}_{21} & 0 \\
0 & 0 & 0 & 0 \\
T^{1}_{21} & 0 & 0 & T^{1}_{22}\\
};
\node[highlight2, fit=(m-1-1.north west) (m-2-2.south east)] {};
\node[highlight1, fit=(m-3-1.north west) (m-4-4.south east)] {};
\end{tikzpicture}
\left[\begin{array}{c}
\phi_{A} \\
\phi_{B} \\
\phi_{C} \\
\phi_{D}
\end{array}\right]
\label{eq:ARphif}
\end{equation}
\begin{equation}
\renewcommand{\arraystretch}{1.5}
B_{R}=
\begin{tikzpicture}[baseline=(m.center)]
\matrix (m) [matrix of math nodes, left delimiter={[}, right delimiter={]},
row sep=1mm, nodes={minimum width=6.5em, minimum height=1.6em}] {
0 & -(T^{2}_{21}+T^{2}_{22}) & 0 \\
0 & 0 & -(T^{3}_{21}+T^{3}_{22})\\
0 & 0 & 0 \\
-(T^{1}_{21}+T^{1}_{22}) & 0 & 0 \\
};
\node[highlight2, fit=(m-1-1.north west) (m-2-3.south east)] {};
\node[highlight1, fit=(m-3-1.north west) (m-4-3.south east)] {};
\end{tikzpicture}
\left[\begin{array}{c}
\phi_{1} \\
\phi_{2} \\
\phi_{3}
\end{array}\right]
\label{eq:BRphii}
\end{equation}
\end{document}
Which produces the following output:
This is extensible by adding character translations. It uses the standard array
environment.
The idea is to change an input such as |bo|x|
into
\multicolumn{1}{|c}{\__fukai_mymacro_char:n{b}} &
\multicolumn{1}{c|}{\__fukai_mymacro_char:n{o}} &
\multicolumn{1}{c|}{\__fukai_mymacro_char:n{x}}
where the last function translates b
into a phantom, just to be sure it occupies the same space as the other characters, o
into \bigcirc
and x
into \bigotimes
.
The search regular expression means: find zero or one occurrences of |
followed by a letter and zero or one occurrences of |
. This is replaced by \multicolumn
as shown above. At the end of this multiple replacement we need to remove an unwanted trailing &
.
The number of columns is computed by examining the arguments and passing +1
for anything that's not |
.
\documentclass{article}
\usepackage{array}
\ExplSyntaxOn
\NewDocumentCommand{\mymacro}{mm}
{
\fukai_mymacro:nn { #1 } { #2 }
}
\tl_new:N \l__fukai_mymacro_a_tl
\tl_new:N \l__fukai_mymacro_b_tl
\cs_new_protected:Nn \fukai_mymacro:nn
{
% normalize the input
\__fukai_mymacro_normalize:Nn \l__fukai_mymacro_a_tl { #1 }
\__fukai_mymacro_normalize:Nn \l__fukai_mymacro_b_tl { #2 }
% start the array with the suitable number of columns
\begin{array}
{
* { \int_max:nn { \__fukai_mymacro_count:n { #1 } } { \__fukai_mymacro_count:n { #1 } } } { c }
}
\tl_use:N \l__fukai_mymacro_a_tl
\\ \hline
\tl_use:N \l__fukai_mymacro_b_tl
\end{array}
}
\cs_new_protected:Nn \__fukai_mymacro_normalize:Nn
{
\tl_set:Nn #1 { #2 }
\regex_replace_all:nnN
{ (\|)??([[:alpha:]])(\|?) } % |<char>|
{ \c{multicolumn}\{1\}\{\1 c \3\}\{ \c{__fukai_mymacro_char:n}\{\2\} \} \& }
#1
\regex_replace_once:nnN { \& \Z } { } #1
}
% count the non-| characters
\cs_new:Nn \__fukai_mymacro_count:n
{
\int_eval:n { 0 + \str_map_function:nN { #1 } \__fukai_mymacro_ischar:n }
}
\cs_new:Nn \__fukai_mymacro_ischar:n
{
\str_if_eq:nnF { #1 } { | } { +1 }
}
% the translations
\cs_new:Nn \__fukai_mymacro_char:n
{
\str_case:nn { #1 }
{
{b}{\phantom{\bigcirc}}
{o}{\bigcirc}
{x}{\bigotimes}
}
}
\ExplSyntaxOff
\begin{document}
$\mymacro{|b|b|ooo|x|}{oooooo}$
\bigskip
$\mymacro{|bo|x|}{o|x|b}$
\bigskip
$
\begin{array}{ccc}
\multicolumn{1}{|c}{} & \multicolumn{1}{c|}{\bigcirc} & \multicolumn{1}{c|}{\bigotimes}
\\\hline
\multicolumn{1}{c|}{\bigcirc} & \multicolumn{1}{c|}{\bigotimes} & \multicolumn{1}{c}{}
\end{array}
$
\end{document}
In the image, the third array is “hand made” to show the result is the same as with the second array done with \mymacro
.
Best Answer
Is this what you mean?
You don't need an array. Just one line of math with over- and under-braces.
It now occurs to me that you may want the overbraces to be inside the delimiters. And I think you want the
*
terms to be potentially bigger (vertically) as in the following:The code is more complicated, but all the delimiters will adjust in size. I made a macro for the 3x1 matrix called
\vertm
just to simplify a bit. Then there is a second macro called\vphnm
that is just the first matrix enclosed in a\vphantom
command. This is necessary to have the delimiters adjust to the matrix and not to the over and under braces.Note Werner's suggestion to use
mathtools
and\mathclap
.Here is the code: