章のタイトルの横に画像を配置するにはどうすればいいですか?

章のタイトルの横に画像を配置するにはどうすればいいですか?

このコードを使用して章のタイトルの横に画像を挿入しようとしています

‎\begin{document}‎
\chapter{for example}
\begin{figure}[h]
\includegraphics[width=20mm]{turing.png}
\end{figure}‎

しかし、画像は新しい行に挿入されます。そこに画像を挿入するための座標を指定できますか?

答え1

TikZを使うこともできます。tikzpagenodesテキスト領域のアンカーを使用して、配置を簡単に制御できます。

\documentclass{book}
\usepackage{graphicx}
\usepackage{tikzpagenodes}
\usepackage{lipsum}

\begin{document}‎

\chapter{Test chapter}
\begin{tikzpicture}[remember picture,overlay]
\node[anchor=east,inner sep=0pt] at (current page text area.east|-0,3cm) {\includegraphics[height=3cm]{example-image-a}};
\end{tikzpicture}

\lipsum[4]

\end{document}

ここに画像の説明を入力してください

答え2

コマンドでグラフィックを指定する場合はchapter、そのコマンドを保護して、toc ファイルを書き込むときに解釈されないようにする必要があります。または、別の見出しを指定します。

\documentclass[12pt, a4paper]{report}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[demo]{graphicx}

\begin{document}
\tableofcontents

\chapter{looks odd in TOC \protect\includegraphics[width=20mm]{test.png}}
\chapter[does not look so odd in TOC (because the graphic is missing)]%
        {does not look so odd in TOC \includegraphics[width=20mm]{test.png}}

\end{document}

答え3

-command の引数内にグラフィックを含めようとすると苦情が寄せられるので\chapter、私の場合は次の方法を試しました。

これは、ハック以上のものですが、次の行にグラフィックを含め、負の値で上に移動することはできます\vspace。任意の単位を使用できますが、垂直方向のスペースの高さには、mm や pt などの絶対単位ではなく、コンテキストで定義されるものを使用するのが最善だと思います。章の本文がキャプションに押し込まれないように、\baselineskip必ず後に正の値を含めてください。\vspace

すべての章にグラフィックを添えたい場合は、これをすべてマクロにまとめることができます。このマクロは、図を参照する 2 番目の引数を必要とする点を除いて、chapter のように動作します。次のようになります。

    \documentclass[final]{book}
\usepackage{graphicx, ifthen}

    \newcommand{\mychapter}[3][\empty]{%
        \ifthenelse{\equal{#1}{\empty}}% check whether optional parameter is empty
                         {\chapter[#2]{#2}}% 
                         {\chapter[#1]{#2}}% 
        {\Huge %
           \vspace{-2.2\baselineskip} % move up
           \hfill % move graphic right
           \includegraphics[height=10mm]{#3} % include graphic
           \vspace{\baselineskip} % move down before body starts
        }% delimit scope of \Huge
    }

\begin{document}‎
\tableofcontents

\mychapter[toc title]{A chapter}{Logo-univie}%different title in TOC and heading

\mychapter{Another chapter}{Logo-univie}%same title everywhere

Some text
\end{document}

注: のオプション引数を保持するように編集されました\chapter

関連情報