GeneralCreating a header for a figure

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
User avatar
tripwire45
Posts: 129
Joined: Thu Apr 10, 2008 4:35 am

Creating a header for a figure

Post by tripwire45 »

Part of the "look and feel" required for the document I'm working on is that various figures need to have headers or titles that "reside" above and to the left of the graphic. I'm using png graphics and have tried to create this effect by simply placing bolded text in a line above where the graphic is thus:

Code: Select all

\textbf{Graphic Header}
\begin{center}
\begin{figure}[!h]
\includegraphics[width=0.75\textwidth]{img/graphic001.png}
 \caption{Graphic caption}
\end{figure}
\end{center}
Even using [!h] there is no absolute guarantee that each and every required header will be placed right where I need it to be and in fact, the net result over a 90 page document, is that the majority of them won't.

Is there a way (and I haven't found one by either Googling or by searching through my available print resources) of including the header text as part of the graphic itself, the way a caption is included? That would be the ideal solution, since the text and graphic would be one unit instead of two units that I'm trying to get to "play nice together".

Thanks.

-Trip

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

Creating a header for a figure

Post by Stefan Kottwitz »

Hi Trip,

do you need header or title above and a caption below? If you just need the text above, you can set the caption above. The caption package is very useful for that generally.
If you put the text outside the figure environment, it is not attached to it.
It would be better to use \centering instead of \begin{center} ... \end{center} because the center environment produces additional vertical space.
Let's see an example:

Code: Select all

\documentclass[a4paper,10pt]{article}
\usepackage{graphicx}
\usepackage[font=bf]{caption}
\begin{document}
\begin{figure}[ht]
\centering
\caption{Graphic caption}
\includegraphics[width=0.75\textwidth]{img/graphic001.png}
\end{figure}
\end{document}
Stefan
User avatar
tripwire45
Posts: 129
Joined: Thu Apr 10, 2008 4:35 am

Re: Creating a header for a figure

Post by tripwire45 »

I'm attaching a png file of what the header and graphic should look like. In this sample, there's a sentence immediately below the header but above the graphic. I could live with that sentence as part of the caption (above or below), but I really need the header to be there, too. I know this may not be "doable", but you never know unless you ask.
Attachments
sample.png
sample.png (23.81 KiB) Viewed 6853 times
User avatar
Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

Creating a header for a figure

Post by Stefan Kottwitz »

You may include the header inside the figure environment:

Code: Select all

\begin{figure}[ht]
{\large\bfseries Deleting}

\begingroup
\centering
\caption{These items help...}
\includegraphics[width=0.75\textwidth]{img/graphic001.png}
\endgroup
\end{figure}
Or you even remove \begin{figure}[ht] ... \end{figure} and do it without floating, in case your graphics should be set right at that certain place.

Stefan
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Creating a header for a figure

Post by localghost »

tripwire45 wrote:[...] Even using [!h] there is no absolute guarantee that each and every required header will be placed right where I need it to be and in fact, the net result over a 90 page document, is that the majority of them won't. [...]
You do the same mistake again as in earlier requests. Give at least the combination of [!ht] or [!hb] for placing floats, otherwise you run into trouble (as you already experienced). Alternatively you can take the advice of Stefan.


Best regards
Thorsten¹
User avatar
tripwire45
Posts: 129
Joined: Thu Apr 10, 2008 4:35 am

Creating a header for a figure

Post by tripwire45 »

The good news is that the graphics stay put. The bad news is that the "header" text seems to be rather mobile. In some tests, it centers itself above the graphic and in others, it places itself below the bottom left corner of the graphic. I've tried both suggestions and they seem to both come up with problems...especially when I'm working with a group of several graphics in a row.

Code: Select all

\begin{figure}[ht]
{\large\bfseries\hspace*{-12pt} Home}
\begingroup
\centering
\includegraphics[width=0.75\textwidth]{img/user005.png}
\endgroup\end{figure}


{\large\bfseries\hspace*{-12pt} CMD}
\begingroup
\centering
\includegraphics[width=0.75\textwidth]{img/user006.png}
\endgroup
EDIT:

About the best I can do at the moment is to create a customized header by using \newcommand in the preamble thus:

Code: Select all

\newcommand{\gph}{\large\bfseries{\hspace*{-12pt}}}
Then create something like this:

Code: Select all

\begin{gph}Help\end{gph}
\begingroup
\centering
\includegraphics[width=0.75\textwidth]{img/user003.png}
\endgroup
User avatar
tripwire45
Posts: 129
Joined: Thu Apr 10, 2008 4:35 am

Re: Creating a header for a figure

Post by tripwire45 »

Solved it. The solution proposed by Stefan worked better in the "live" document than in my test doc. Things are looking up. Thanks again.
Post Reply