0
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?

More than 1 year has passed since last update.

[LaTeX]簡単に画像を(並べて)挿入するマクロ

Last updated at Posted at 2022-05-30

LateX(pLateX)を使っていると,画像を挿入するだけで何行も書かなくてはならずその面倒臭さに辟易とするでしょう.

環境


%図を入れる用
\usepackage[dvipdfmx]{graphicx}
\usepackage[utf8]{inputenc}
\usepackage{color}
\usepackage{comment}
\usepackage{amsmath}
\usepackage{listings}[langage=python3]
\usepackage{jlisting}[langage=python3]
\usepackage{url}
\usepackage{titlesec}
\usepackage{multirow}
\usepackage{tabularx}
\usepackage{diagbox}
\usepackage{aliascnt}
\usepackage{umoline}
\usepackage{pdfpages}
%これがないと警告がでてうざい
\makeatletter
\def\caption@documentclass{elsarticle}%
\makeatother
\usepackage[hang,small,bf]{caption}
\usepackage{ifthen,calc}
\usepackage{subcaption}
\usepackage{float}
\usepackage{amssymb}
\usepackage{hyperref}
\hypersetup{
  setpagesize=false,
  bookmarksnumbered=true,
  bookmarksopen=true,
  colorlinks=true,
  linkcolor=blue,
  citecolor=red,
}
\usepackage[noabbrev]{cleveref}
% crefをrefと見せかける
% hyperrefと併用できない感じか?
\let\nref\ref
\let\ref\cref

\captionsetup{compatibility=false} 
\renewcommand{\thesubfigure}{\alph{subfigure}}
%ソースコードをプログラムと読み替える
\renewcommand{\lstlistingname}{プログラム}
\def \pgpath{../../..}

%丸文字
\newcommand{\onum}[1]{\raise0.2ex\hbox{\textcircled{\scriptsize{#1}}}}
%ロリバさんの囲み文字大きさ固定化
\newcommand{\mybox}[1]{\framebox[1.2em]{\rule{0pt}{1.6ex}#1}}
%余白
\usepackage[top=7truemm,bottom=20truemm,left=20truemm,right=20truemm]{geometry}
\renewcommand{\thesection}{\arabic{section}.}
\renewcommand{\thesubsubsection}{\alph{subsubsection}.}
\makeatletter
%%%%% lstlistingのスタイル定義 ここから
\lstdefinestyle{ex}{
 frame={trbl},
 frameround={tttt},
 lineskip=-0.7ex,
 numbers=none
}


\lstset{%default
  language=Python,
  basicstyle={\ttfamily},
 breaklines=true,
 escapechar=\`,
 columns=[l]{fullflexible},
 keepspaces=true,
 showstringspaces=false,
 xrightmargin=0em,
 xleftmargin=1em,
 stepnumber=1,
 lineskip=-0.5ex,
 frame={lRtB},
 frameround={nnnn},
 numberstyle={\scriptsize},
 numbers=left
}
%tab内でfootnotemarkを使う
\makeatletter
\def\@xfootnote[#1]{%
  \protected@xdef\@thefnmark{#1}%
  \@footnotemark\@footnotetext}
\makeatother

%\captionsetup[subfigure]{labelformat=simple}
\renewcommand{\thesubfigure}{\alph{subfigure}}
% 参照フォーマット
\crefname{equation}{}{}
\crefname{figure}{}{}
\crefname{subfigure}{}{}
\crefname{table}{}{}
\crefname{subtable}{}{}
\crefname{appendix}{付録}{付録}
\crefname{subappendix}{付録}{付録}
\crefname{subsubappendix}{付録}{付録}
\crefname{subsubsubappendix}{付録}{付録}
\crefformat{lstlisting}{{プログラム}#2#1#3}
\crefrangeformat{lstlisting}{{プログラム}#3#1#4{--}#5#2#6}
\crefmultiformat{lstlisting}{{プログラム}#2#1#3}{, {プログラム}#2#1#3}{, #2#1#3}{, {プログラム}#2#1#3}

\begin{comment}
\crefformat{section}{#2#1#3{}}
\crefrangeformat{section}{#3#1#4{--}#5#2#6{}}
\crefmultiformat{section}{#2#1#3{}}{, #2#1#3{}}{, #2#1#3}{, #2#1#3{}}
\crefformat{subsection}{#2#1#3{}}
\crefrangeformat{subsection}{#3#1#4{--}#5#2#6{}}
\crefmultiformat{subsection}{#2#1#3{}}{, #2#1#3{}}{, #2#1#3}{, #2#1#3{}}
\crefformat{subsubsection}{#2#1#3{}}
\crefrangeformat{subsubsection}{#3#1#4{--}#5#2#6{}}
\crefmultiformat{subsubsection}{#2#1#3{}}{, #2#1#3{}}{, #2#1#3}{, #2#1#3{}}
\end{comment}
%-------------自分用--------------
%---------------------------------
%チルダ~
\def \tilde{$\sim$}

%空行-----------------
\newcommand{\lnskip}{\vskip\baselineskip}
%img[htbp(H)]{filename}{caption}{(fig:)label}{scale}------------------
\newcommand{\img}[5][htbp]{%
	\begin{figure}[#1]%
		\centering%
		\includegraphics[scale=#5]{#2}%
		\caption{#3}%
		\label{#4}%
	\end{figure}%
}
%someimg[option]{filename (1~.png)}{caption}{(fig:)label(1~)}{N個}{折り返し}{size}---------------------  
\newcommand{\filepath}[1]{#1}
%--------------------
%\someimg[scale]{filename}{caption}{(fig:)label}{pagesize}
\newcommand{\someimg}[5][]{%
	\begin{minipage}{#5\linewidth}%
		\centering
		\includegraphics[#1]{#2}%
		\subcaption{#3}%
		\label{#4}%
	\end{minipage}%
}
%--------------------
%表コマンド\begin{tab}{caption}{label}{lll}
\newenvironment{tab}[4][htbp]{\begin{table}[#1]\centering\caption{#2}\label{#3}\begin{tabular}{#4}}{\end{tabular}\end{table}}
%複数表 \begin{sometab}{caption}{tab:label}{lll}{pagesize} \end{sometab}
\newenvironment{sometab}[4]{\begin{minipage}[c]{#4\linewidth}\centering\subcaption{#1}\label{#2}\begin{tabular}{#3}}{\end{tabular}\end{minipage}}



\newenvironment{m}{\begin{equation*}\begin{split}}{\end{split}\end{equation*}}

\newenvironment{e}{\begin{equation}}{\end{equation}}
%------------------------------------------
%------------------------------------------
%sectionを1.というふうにする用
  \def\@seccntformat#1{%
  \@nameuse{@seccnt@prefix@#1}%
  \@nameuse{the#1}%
  \@nameuse{@seccnt@postfix@#1}%
  \@nameuse{@seccnt@afterskip@#1}}
  %-------------------------
  %sectionのあとのピリオド
  \def\@seccnt@prefix@section{}
  \def\@seccnt@postfix@section{.}
  \def\@seccnt@afterskip@section{\hskip1zw}
  %-----------------------------
  %subsub
  \makeatother

よくわからない人はなにも考えずこれをプリアンブルに貼っつけよう!
プリアンブル長いのが嫌な人はstyファイルをgithubにあげているのでこれをダウンロードして\usepackageで参照しても良いです.
https://github.com/soburi59/pLaTeX/blob/main/mystyle.sty

画像挿入

以下のようにつかう.
scaleは拡大する倍率の数字を入れれば良い.
1で等倍.

\img[htbp]{test.png}{caption}{fig:label}{scale}

表示

fig1.png

複数画像挿入

\begin{figure}[htbp]
   \someimg[scale=0.35]{test1.png}{BATCHSIZE=32}{fig:b32}{0.33}
   \someimg[scale=0.35]{test2.png}{BATCHSIZE=64}{fig:b64}{0.33}
   \someimg[scale=0.35]{test3.png}{BATCHSIZE=128}{fig;b128}{0.33}\\ %←これで改行
   \someimg[scale=0.35]{test4.png}{BATCHSIZE=256}{fig:b256}{0.33}
   \someimg[scale=0.35]{test5.png}{BATCHSIZE=512}{fig:b512}{0.33}
   \someimg[scale=0.35]{test6.png}{BATCHSIZE=1024}{fig:b1024}{0.33}

   \caption{バッチサイズごとのグラフ}
   \label{fig:b}
\end{figure}

最後の引数はminipageの大きさです.横に2つなら0.45,3つなら0.33,4つなら0.23などがおすすめです.

表示

image.png

参考にしたのはloliverさんのこちらのサイトです.
https://blog.loliver.net/2020/08/12/latex-tutorial-figures

0
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
0
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?