0

I'm helping my gf write her first LaTeX report and her teacher insists on some rules regarding images.

  • The caption of the image has to float to the left
  • There has to be a short text about the origin of the picture (floating right) between every picture and its caption
  • There can be no extra space between the image and the right floating text
  • There can be no extra space between the right floating text and the caption

The image (created with Microsoft Word) hopefully clarifies things a bit. All we need is to get the same results with LaTeX :) enter image description here

  • Welcome to TeX.SX! Have a look at the caption package and its capabilities to define own caption styles. – TeXnician Feb 11 '17 at 18:14
  • Welcome to TeX.SX! On this site, a question should typically revolve around an abstract issue (e.g. "How do I get a double horizontal line in a table?") rather than a concrete application (e.g. "How do I make this table?"). Questions that look like "Please do this complicated thing for me" tend to get closed because they are either "off topic", "too broad", or "unclear". Please try to make your question clear and simple by giving a minimal working example (MWE): you'll stand a greater chance of getting help. – Reinstate Monica - M. Schröder Feb 11 '17 at 19:46
2

First approach

Intro

I'd suggest in this case, that you define a new macro, to ease up the input of your images.

In my example, I defined \myfigure, which takes 3 parameters:

  1. width of the image and caption
  2. name of the image file
  3. Source Text
  4. Caption text

To have a better word wrapping on the caption and source line, I added package ragged2e. It enables wrapping in the middle of words, not just only between words.

As demanded by the teacher, I defined \parskip to be 0ex, to prevent white space between the text lines. As I don't know, what may globally defined in your document, I enclosed it in a \begingroup ... \endgroup, it won't interfere with the global setting.

After having set the width of the \parbox, you must not define the width of the image with #1, too. I defined it as [width=\linewidth] instead. \linewidth is of course the width of the surrounding \parbox.

I also added a label, to reference the image correctly.

Code

This is the MWE:

\documentclass{article}
\usepackage{graphicx}
\usepackage{ragged2e}           % for better word wrapping
\usepackage{lipsum}             % for dummy text

%% newcommand, to make the insertion of captions easier.
\newcommand{\myfigure}[4]{%
  \centering
  \begingroup
  \setlength{\parskip}{0ex}\setlength{\parindent}{0pt}
  \parbox[t]{#1}{%
    \includegraphics[width=\linewidth]{#2}\par
    \RaggedLeft Source:\ #3\par
    \RaggedRight #4\par}
  \endgroup
}

\begin{document}
\lipsum[4]
\begin{figure}[ht]
  \myfigure{0.7\linewidth}{example-image}{www.dummy.com}{This is the caption beneath
    the image, hopefully typeset flush left.}
  \label{fig:example}
\end{figure}
\end{document}

Result

This is the result:

First example result

I hope, everything works as expected.

Second approach

Intro

If you like, you could go on step further and define a complete environment. That will be a good exercise and will save some typing labour from than on.

Instead of defining \myfigure as a command, I will now define the myfigure environment. It replaces the LaTeX figure environment. It takes the same four arguments, as the command in the above example. Note: the arguments #1 till #9 can only be used in the \begin{myfigure} part of the environment. If you want use them in the \end{myfigure}-part, you have to store them into an local macro in \begin-part. Afterwards, you are able to call them in the \end-part. (In this case, this is not necessary.)

Code

\documentclass{article}
\usepackage{graphicx}
\usepackage{ragged2e}           % for better word wrapping
\usepackage{lipsum}             % for dummy text

%% new environment, to replace the LaTeX figure environment
\newenvironment{myfigure}[4]{%
  \begin{figure}
    \centering
    \setlength{\parskip}{0ex}\setlength{\parindent}{0pt}
    \parbox[t]{#1}{%
      \includegraphics[width=\linewidth]{#2}\par
      \RaggedLeft Source:\ #3\par
      \RaggedRight #4\par}}{%
    \end{figure}}

\begin{document}
\lipsum[4]
\begin{myfigure}{0.7\linewidth}{example-image}{www.dummy.com}{This is
    the caption beneath the image, hopefully typeset flush left.} 
  \label{fig:example}
\end{myfigure}
See the figure~\ref{fig:example}.
\end{document}

Result

Believe it or not, the result will be the same. :-)

  • Thank you very much! I really like your second solution! Follow up: Is it possible to add a counter to the myfigure environment? Example: Figure 1: This is a caption below the image [I've been browsing around about custom counters and it seems to be a bit outside of my current LaTeX abilities (improving.] – Markús Torfason Feb 12 '17 at 20:39
3

Like this?

enter image description here

\documentclass{article}
\usepackage{stackengine}
\usepackage{graphicx}

\usepackage[font=small]{caption}
\newlength\myfigurewidth % for defining caption and image width

\usepackage{lipsum}% for dummy text

\begin{document}
\lipsum[4]
    \begin{figure}[ht]
    \centering
\setlength\myfigurewidth{3in}
\captionsetup{singlelinecheck=off, width=\myfigurewidth, skip=1pt}
\def\stackalignment{r}
\stackunder{\includegraphics[width=\myfigurewidth]{example-image}}%
           {\small%
            Source: www.stackexchange.com}
\caption{Map of Reykjavík.}
    \end{figure}
\end{document}

Note: answer is based on ideas of answers on question footnote-to-figure. The first line below image had to be shorter than figure width, otherwise it will spill-off on left side of the image. If needed, you can use \url{...} for linking to source of image:

\stackunder{\includegraphics[width=\myfigurewidth]{example-image}}%
           {\small%
            Source: \url{www.stackexchange.com}}

in this case you should add to preamble \usepackage{url}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.