如何在章節標題旁放置圖片?

如何在章節標題旁放置圖片?

我正在嘗試使用此程式碼在章節標題之外插入圖片

‎\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。您可以使用任何單位,但我認為最好使用\baselineskip上下文定義的單位,而不是使用 mm 或 pt 等絕對單位來表示垂直空間的高度。一定要\vspace在後面加上正面的內容,以避免將章節正文擠到標題上。

如果您想要一個圖形與所有章節相匹配,您可以將所有這些打包到一個宏中,該宏的行為類似於章節,只是它需要引用圖形的第二個參數,如下所示:

    \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

相關內容