1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【LaTeX】インライン数式の行列が変なので,なんとかしよう

1
Last updated at Posted at 2026-01-15

LaTeXで行列を書くとき,普通は以下のように記述するかと思います.

\begin{pmatrix}
    a & b \\
    c & d \\
\end{pmatrix}

これによる出力は次のようになります.

\begin{pmatrix}
    a & b \\
    c & d \\
\end{pmatrix}

これはまったくもって問題ないのですが,これを displaystyle にして文中数式で使うと,私の環境(LuaLaTeX)では画像のようになってしまいました.

スクリーンショット (81).png

画像元PDFのTeXソース
% プリアンブルや\begin{document}などは省略

\centering インライン $\displaystyle \begin{pmatrix} a & b \\ c & d \end{pmatrix}$ インライン

\[
\text{比較用の別行立て}
\begin{pmatrix}
  a & b \\
  c & d
\end{pmatrix}
\text{比較用の別行立て}
\]

括弧のサイズが妙に大きくてダサいですね. これを何とかしましょう.


なんとかした(かった)結果

その1. とりあえずChatGPTに実装してもらった

\newcommand{\inlinematt}[4]{% インライン数式でpmatrix
  \displaystyle% 消すとうまくいかない
  \left(%
  \vphantom{0 \atop 0}% 括弧サイズ調整用vphantom
    \raise0.56pt\hbox{$% ちょっと上にずらしたかった
      \vcenter{%
        \offinterlineskip% 自動の行間を切る
        \halign{\hfil$\textstyle##$\hfil&\hfil$\textstyle##$\hfil\cr
          \vphantom{0}\mathord#1 \ \,&\,\  #2\cr
          \noalign{\vskip 5.2pt}% 1行目と2行目の行間
          \vphantom{0}\mathord#3 \ \,&\,\  #4\cr
        }%
      }%
    $}%
  \right)%
}

おぞましいもの すばらしいコマンドが完成しました! いや~最近のAIは便利ですね

たとえば,

ここで,行列 \( A = \inlinematt{1}{2}{3}{4} \) は正則で,

とすれば

スクリーンショット (82).png

と,いい感じのサイズになりましたね.ところが,この \inlinematt には致命的な弱点があって

スクリーンショット (83).png

マイナス記号を付けるとなぜか成分の位置する高さが変わってしまいます.(左上の $1$ と括弧の位置関係を見るとわかりやすい.)マイナスがというより,文字の高さが原因みたいです.アルファベット大文字もこうなります.


その2. じゃあそれを気合で調整しよう

先に断っておきますが, 自動調整ではありません.完全に手動です.

\newcommand{\inlinematta}[6]{% 行間と高さを変更可能
  \displaystyle%
  \left(%
  \vphantom{0 \atop 0}%
        \raise\dimexpr 0.56pt + #1\relax\hbox{$% #1 : 高さの変化分.単位 "pt" を忘れずに
      \vcenter{%
        \offinterlineskip%
        \halign{\hfil$\textstyle##$\hfil&\hfil$\textstyle##$\hfil\cr
          \vphantom{0}\mathord#3 \ \,&\,\  #4\cr
          \noalign{\vskip\dimexpr 5.2pt + #2\relax}% #2 : 行間の変化分.
          \vphantom{0}\mathord#5 \ \,&\,\  #6\cr
        }%
      }%
    $}%
  \right)%
}

気合で調整をすることにより

行列 \( B = \inlinematta{-0.75pt}{-0.75pt}{1}{-2}{3}{-6} \) は正則でない.

スクリーンショット (84).png

とさっきより見た目がマシになりました! なんとこの調整に7分くらいかかっています! 時間の無駄!


結論

インラインに行列を書くのは諦めろ!(爆発オチ)

普通に諦めて別行立てにしたほうがいいです.それか,なんかいい感じのパッケージが存在していればそれを使いましょう.

一応コピペ用に置いておきます.vmatt と dett は同じです.

\newcommand{\inlinematt}[4]{\displaystyle\left(\vphantom{0 \atop 0}\raise0.56pt\hbox{$\vcenter{\offinterlineskip\halign{\hfil$\textstyle##$\hfil&\hfil$\textstyle##$\hfil\cr\vphantom{0}\mathord#1 \ \,&\,\  #2\cr\noalign{\vskip 5.2pt}\vphantom{0}\mathord#3 \ \,&\,\  #4\cr}}$}\right)}
\newcommand{\inlinevecc}[2]{\displaystyle\left(\vphantom{0 \atop 0}\raise0.56pt\hbox{$\vcenter{\offinterlineskip\halign{\hfil$\textstyle##$\hfil&\hfil$\textstyle##$\hfil\cr \vphantom{0}\mathord#1\cr\noalign{\vskip 5.2pt} \vphantom{0}\mathord#2\cr}}$}\right)}
\newcommand{\inlinevmatt}[4]{\displaystyle\left|\vphantom{0 \atop 0}\raise0.56pt\hbox{$\vcenter{\offinterlineskip\halign{\hfil$\textstyle##$\hfil&\hfil$\textstyle##$\hfil\cr \vphantom{0}\mathord#1 \ \,&\,\  #2\cr\noalign{\vskip 5.2pt} \vphantom{0}\mathord#3 \ \,&\,\  #4\cr}}$}\right|}
\newcommand{\inlinedett}[4]{\displaystyle\left|\vphantom{0 \atop 0}\raise0.56pt\hbox{$\vcenter{\offinterlineskip\halign{\hfil$\textstyle##$\hfil&\hfil$\textstyle##$\hfil\cr \vphantom{0}\mathord#1 \ \,&\,\  #2\cr\noalign{\vskip 5.2pt} \vphantom{0}\mathord#3 \ \,&\,\  #4\cr}}$}\right|}

\newcommand{\inlinematta}[6]{\displaystyle\left(\vphantom{0 \atop 0}\raise\dimexpr 0.56pt + #1\relax\hbox{$\vcenter{\offinterlineskip\halign{\hfil$\textstyle##$\hfil&\hfil$\textstyle##$\hfil\cr\vphantom{0}\mathord#3 \ \,&\,\  #4\cr\noalign{\vskip\dimexpr 5.2pt + #2\relax}\vphantom{0}\mathord#5 \ \,&\,\  #6\cr}}$}\right)}
\newcommand{\inlinevecca}[4]{\displaystyle\left(\vphantom{0 \atop 0}\raise\dimexpr 0.56pt + #1\relax\hbox{$\vcenter{\offinterlineskip\halign{\hfil$\textstyle##$\hfil&\hfil$\textstyle##$\hfil\cr \vphantom{0}\mathord#3\cr\noalign{\vskip\dimexpr 5.2pt + #2\relax} \vphantom{0}\mathord#4\cr}}$}\right)}
\newcommand{\inlinevmatta}[6]{\displaystyle\left|\vphantom{0 \atop 0}\raise\dimexpr 0.56pt + #1\relax\hbox{$\vcenter{\offinterlineskip\halign{\hfil$\textstyle##$\hfil&\hfil$\textstyle##$\hfil\cr \vphantom{0}\mathord#3 \ \,&\,\  #4\cr\noalign{\vskip\dimexpr 5.2pt + #2\relax} \vphantom{0}\mathord#5 \ \,&\,\  #6\cr}}$}\right|}
\newcommand{\inlinedetta}[6]{\displaystyle\left|\vphantom{0 \atop 0}\raise\dimexpr 0.56pt + #1\relax\hbox{$\vcenter{\offinterlineskip\halign{\hfil$\textstyle##$\hfil&\hfil$\textstyle##$\hfil\cr \vphantom{0}\mathord#3 \ \,&\,\  #4\cr\noalign{\vskip\dimexpr 5.2pt + #2\relax} \vphantom{0}\mathord#5 \ \,&\,\  #6\cr}}$}\right|}
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?