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?

【MATLAB 可視化 Tips】すべての Interpreter を一括で LaTeX に変更する裏技

Last updated at Posted at 2025-09-25

はじめに

MATLAB では軸ラベルや凡例,タイトルなどの Interpreter として LaTeX を指定すれば数式をカッコよく表示できます.

% 例:ylabel に数式を書く場合
ylabel("$f(x)=\sin(x)$", "interpreter", "latex")

けど,要素ごとに毎回 Interpreter を指定するのは面倒...🥲

そこで本記事では,すべての Interpreter を一括で LaTeX に指定する裏技 を紹介します

コピペ用コード

以下のコードをグラフを作成するコードの前にコピペすれば,概ねすべての要素の Interpreter が LaTeX に変更されます.

defaults = fieldnames(get(groot, "factory"));
interpreterSettings = defaults(contains(defaults, "Interpreter"));
for i = 1:numel(interpreterSettings)
    set(groot, replace(interpreterSettings{i}, "factory", "default"), "latex")
end

仕組み

仕組みは以下の通りです.

  1. fieldnames(get(groot,"factory")) で,MATLAB 出荷時の規定値(factory settings)の一覧を取得
  2. その中から "Interpreter" を含む項目を検索
  3. それらを for で順番に処理し,すべての Interpreter の既定値(default settings)を "latex" に変更

これで,軸ラベル(xlabel, ylabel),タイトル(title),凡例(legend)などの Interpreter の規定値が LaTeX 変更されます.

※ 参考:既定のプロパティ値

使用例

% Interpreter の一括変更
defaults = fieldnames(get(groot, "factory"));
interpreterSettings = defaults(contains(defaults, "Interpreter"));
for i = 1:numel(interpreterSettings)
    set(groot, replace(interpreterSettings{i}, "factory", "default"), "latex")
end

% 以降は Interpreter を指定しなくても LaTeX の数式を書ける!
plot(linspace(0,2*pi),sin(linspace(0,2*pi)))
xlabel("$x$")
ylabel("$f(x)=\sin(x)$")
xticks([0 pi 2*pi])
xticklabels(["$0$", "$\pi$" "$2\pi$"])
yticks([-1 -0.5 0 0.5 1])
yticklabels(["$-1$", ...
             "$-\displaystyle\frac{1}{2}$", ...
             "$0$", ...
             "$\displaystyle\frac{1}{2}$", ...
             "$1$"]);
grid minor
xlim("tight")
ylim("padded")
title("Interpreter \LaTeX")
fontsize(12,"points")

image.png

注意:個別の指定が必要なケース

以下の項目は一括指定の対象外です(他にもあるかもしれません)

  • colorbar のラベル
  • tiledLayout を target とした xlabelylabel

これらの対象に対しては手動で Interpreter を設定する必要があるようです.

ちなみに colorbar のラベルの Interpreter は以下のコードで設定します.

cb                   = colorbar;   % colorbar のハンドルを取る
cb.Label.String      = "$ax + b$"; % ラベルの設定
cb.Label.Interpreter = "latex";    % Interpreter の設定

設定を元に戻す方法

設定を元に戻したい場合は,"remove" を使って規定値を削除すれば OK です.

defaults = fieldnames(get(groot, "factory"));
interpreterSettings = defaults(contains(defaults, "Interpreter"));
for i = 1:numel(interpreterSettings)
    set(groot, replace(interpreterSettings{i}, "factory", "default"), "remove")
end

まとめ

この記事では Interpreter を一括で LaTeX に設定する方法を紹介しました.毎回の Interpreter の指定が不要になり大変便利です.ぜひご活用ください!

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?