スキップしてメイン コンテンツに移動

tikzで行列の積を表現してみる

行列の積の成分計算を表す図をtikzで作成してみました。元ネタは佐武線形代数です。
画像は次の通り。
LaTeXのコードは次のようになりました。
行列の外の括弧を表現するのにdecorations.pathreplacingライブラリを,
行列を上部で揃えるのにpositioningライブラリを使っています.
色付けはbackgroundsライブラリを使い,scope環境にon background layerオプションを付けて文字の背後に色を付けるようにしました.

\documentclass[a4paper,dvipdfmx,uplatex]{jsarticle}

\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{decorations.pathreplacing}
\usetikzlibrary{matrix,backgrounds,fit,positioning}

\begin{document}
\begin{equation*}
\begin{tikzpicture}[every left delimiter/.style={xshift=1ex},every right delimiter/.style={xshift=-1ex}]
%
\matrix(A)[matrix of math nodes,nodes in empty cells,
 ampersand replacement=\&,
 left delimiter={[},right delimiter={]},
 inner sep=0.8ex,
 ]
 {
 \phantom{b_{11}}\&\hphantom{\dotsb}\&\phantom{\dotsb}\&a_{1j}\&\phantom{b_{1n}}\\
 \phantom{a_{11}}\&\&\&a_{2j}\&\phantom{a_{1n}}\\
 \phantom{\mathstrut}\&\&\&\phantom{\mathstrut}\&\\
 \phantom{\mathstrut}\&\&\&\phantom{\mathstrut}\&\\ 
 \phantom{a_{11}}\&\&\&a_{mj}\&\phantom{a_{1n}}\\
 };
 \draw[line width=1pt,line cap=round,dash pattern=on 0pt off 4\pgflinewidth](A-3-4.north)--(A-4-4.south);
 \draw[decorate,decoration={brace,amplitude=5}]
($(A-1-1.north west)+(-0.5ex,1.5ex)$)--($(A-1-5.north east)+(0.5ex,1.5ex)$)
 node[midway,yshift=2.5ex]{$n$};
 \draw[decorate,decoration={brace,mirror,amplitude=5}]
($(A-1-1.north west)+(-1.8ex,0.2ex)$)--($(A-5-1.south west)+(-1.8ex,-0.2ex)$)
 node[midway,xshift=-2.5ex]{$m$};
 \begin{scope}[on background layer]
 \fill[gray!40,rounded corners](A-1-4.north west) rectangle (A-5-4.south east);
 \end{scope} 

 \matrix(B)[matrix of math nodes,nodes in empty cells,
 ampersand replacement=\&,left delimiter={[},right delimiter={]},inner sep=0.8ex,
 left=0.8cm of A.north west,
 anchor=north east]
 {
 \phantom{b_{11}}\&\&\&\&\phantom{b_{11}}\\
 b_{h1}\&b_{h2}\&\vphantom{b_{h1}}\phantom{\dotsb}\&\phantom{\dotsb}\vphantom{b_{h1}}\&b_{h m}\\
 \phantom{b_{11}}\&\&\&\&\\
 \phantom{b_{11}}\&\&\&\&\\
 };
 \node[inner sep=0pt,fit=(B-2-3)(B-2-4)](fit1){};
 \node[baseline=(fit1.base)]at (fit1){$\dotsb\dotsb$};
 \draw[decorate,decoration={brace,amplitude=5}]
($(B-1-1.north west)+(-0.5ex,1.5ex)$)--($(B-1-5.north east)+(0.5ex,1.5ex)$)
 node[midway,yshift=2.5ex]{$m$};
 \draw[decorate,decoration={brace,mirror,amplitude=5}]
($(B-1-1.north west)+(-1.8ex,0.2ex)$)--($(B-4-1.south west)+(-1.8ex,-0.2ex)$)
 node[midway,xshift=-2.5ex]{$l$};
 \begin{scope}[on background layer]
 \fill[gray!40,rounded corners](B-2-1.south west) rectangle (B-2-5.north east);
 \end{scope} 

\matrix(BA)[matrix of math nodes,nodes in empty cells,
 ampersand replacement=\&,
 left delimiter={[},right delimiter={]},
 inner sep=0.8ex,
right=1.4cm of A.north east,
anchor=north west
]
 {
 \phantom{b_{11}}\&\phantom{\dotsb}\&\phantom{\dotsb}\&\phantom{b_{hj}}\&\phantom{b_{11}}\\
 \phantom{c_{11}}\&\phantom{c_{11}}\&\&\node[fill=gray!40,rounded corners]{c _{hj}};\&\phantom{c_{11}}\\
 \phantom{b_{11}}\&\&\&\phantom{c_{11}}\&\\
 \phantom{b_{11}}\&\&\&\phantom{b_{11}}\&\phantom{b_{11}}\\
 };
 \draw[decorate,decoration={brace,amplitude=5}]
($(BA-1-1.north west)+(-0.5ex,1.5ex)$)--($(BA-1-5.north east)+(0.5ex,1.5ex)$)
 node[midway,yshift=2.5ex]{$n$};
 \draw[decorate,decoration={brace,mirror,amplitude=5}]
($(BA-1-1.north west)+(-1.8ex,0.2ex)$)--($(BA-4-1.south west)+(-1.8ex,-0.2ex)$)
 node[midway,xshift=-2.5ex]{$l$};
\begin{scope}[on background layer]
 \draw[line width=1pt,line cap=round,dash pattern=on 0 pt off 4\pgflinewidth]
(BA-1-4.north)--(BA-4-4.south) (BA-2-1.west)--(BA-2-5.east);
\end{scope}
\node at($(A.east)+(10pt,15pt)$){$=$};
\node[below=0.5cm of A](A2){$A$};
\node(B2)at(B |- A2){$B$};
\node(BA2)at(BA |- A2){$BA$};
\draw[line width=1pt,line cap=round,dash pattern=on 0 pt off 4\pgflinewidth](B)--(B2) (A)--(A2) (BA)--(BA2);
\end{tikzpicture}
\end{equation*}
\end{document}

このブログの人気の投稿

OpenCv-PythonとpdfLaTeXで自炊pdfファイルの位置調整

この記事は TeX & LaTeX Advent Calendar 2021 の11日目の記事です. 10日目は t_kemmochi さん,12日目は yukishita さんです.   動機  書籍の自炊にブックエッジスキャナーの AvisionのFB2280E を利用しています。書籍を裁断せずに1ページずつスキャンを行うため、本の開き具合などの要因で本の序盤、中盤、終盤で得られるスキャン画像の位置ずれが大きくなります。以前からpdfLaTeXを利用して位置調整をしていたのですが、OpenCvを利用して自動化できそうだったので試してみました。 やること 本記事では、pdf化した文書画像の版面(文字の印刷された部分の意味で使います)を半自動的に計算してpdfの画像の位置調整をするために、OpenCvとpdfLaTeXを利用する方法を解説します。手順としては Pythonの画像認識用ライブラリであるOpenCvを利用して版面を計算する。 テンプレートエンジンライブラリのJinja2を使って画像のバウンディングボックスなどを記述したLaTeXファイルを作成する。 pdfLaTeXでpdfを読み込み、位置の調整されたpdfを作成する。 という流れになっています。作業環境としては、 Visual Studio CodeをRemove - WSL拡張機能と併用 しています。PythonやそのライブラリはWSL上のUbuntuにインストールし、他にもpdfファイルを他のファイル形式にするためにpoppler-utilsなんかもインストールしました。 参考記事 OpenCvを利用した文書画像のレイアウト解析については ブログSomething Like Programming内の記事 Document Layout Analysis githubリポジトリ rbaguila/document-layout-analysis OpenCv-Pythonチュートリアル を参考にしました。 pdfLaTeXがpdf加工に使えるという話と、pdfpagesパッケージの存在は doraTeXさんのブログTeX Alchemist Onlineの記事 pdfTeX による見開きPDFの結合・分割 で知ったように思います。 画像pdfをpdfpagesで取り込む ま...

定理環境をmdframedからtcolorboxへ乗り換え

追記:2016/05/02 以前に掲載していた定理環境のマクロでは、カウンターが変化しないような記述をしてしまっていたので、全面的に書き換えました。 追記:2016/06/06 tcolorboxのオプションを追加できるように書き直しをしました( tcolorboxを使った定理環境 )。 LaTeXの話です。 これまで定理環境の修飾にmdframed.styを利用してきました。 mdframedを用いた定理環境の修飾(platex+dvipdfmx, tikz) しかし上の投稿で用いた記述をしていると、定理環境がページの下部にきたとき、まれに以下のように環境が新しいページに送られてしまい変なところで改ページが起こります。 TeX-LaTeX Stack Exchangeでも同様の不具合が報告されていて、mdframed.styの作者が回答して解決しているようなのですが、私の場合は上手く行きませんでした。タイトル部分の位置の計算に問題があるようです。 そういう訳で、mdframed.styよりも高い表現力を持つtcolorbox.styには興味を持っていました。今回は、mdframed.styで記述していた部分を、ソースの変更無しにtcolorboxで置き換えてみたので、その紹介をします。 tcolorbox.styで置き換えた結果は次のようになりました。 見た目はほとんど変わっていません。中のboxは別のtcolorbox環境です。 この定理環境のソースコードは次のようになります。 \usepackage{tcolorbox} \usepackage{varwidth} \tcbuselibrary{breakable} \tcbuselibrary{skins} \definecolor{frameinnercolor}{RGB}{49,44,44} \newcounter{theorem} \numberwithin{theorem}{section}% numberwithinはamsmath.styで定義されている \newenvironment{theorem}[2][]{% %#1 = タイトル, #2 = 定理環境名 \refstepcounter{theorem}% \newtcolorbox{...

定理を再掲したときに定理番号も揃える(amsmathのtagと似た挙動の定理環境を作る).

LaTeXの話題です.amsmath.styで定義されているtagコマンドは,数式環境の式番号部分を自由な文字列で置き換えることができるようになっています.下に例を挙げます. この画像を生成するLaTeXのコードは次のようになります. \documentclass[a4paper,dvipdfmx,uplatex]{jsarticle} \usepackage[dvipdfmx,bookmarks=true,bookmarksnumbered=true,% ,colorlinks=true,linkcolor=blue]{hyperref}% \usepackage{showkeys} \usepackage{amsmath} \numberwithin{equation}{section} Unknown environment 'document' 最初のequation環境にeq:eulerというlabelをつけ,第2のequation環境ではtagコマンドを使って数式番号部分に¥ref{eq:euler}と書くことで,実際に表示される式番号が第1のequation環境のものと同じになっています.この第2のequation環境にはeq:euler2というlabelを付けており,これを¥eqref{eq:euler2}と参照すると表示される番号は第1のequation環境のものであるにも関わらず,リンクをクリックすると第2のequation環境にジャンプします. このような現象が起こるのは,上記のファイルをタイプセット...