2

I've been going through some LaTeX today and I'm wondering how to get a custom function that will display images.

What I wanted to do was have custom functions for each chapters images that would lead to the correct directory and enable me to caption the picture.

I'm trying to center the text on the image (I think that might be best practice for image captions?)

But here's the function at the moment :

\newcommand{\qweq}[3]{
\begin{figure}
\centering
\includegraphics[width=9cm]{/images/task#1/#2}
\caption{
\emph{
\small{
#3
}
}
}

I'm not sure too sure how to center the text, at the moment I'm struggling to get it to differentiate itself much from the text body. It's a bit smaller and italics but when looking at it it doesn't seem clean enough. Here's an image though, any advice welcome :enter image description here

  • \usepackage{caption} and \captionsetup[figure]{justification=centering} – user11232 Dec 17 '14 at 2:15
  • When you say "center the caption on the figure", do you mean that the caption width is the same as that of the figure width? If so, the boxhandler package can do this automatically. – Steven B. Segletes Dec 17 '14 at 2:15
  • Welcome to TeX.SX! Please help us to help you and add complete your code to provide a minimal working example (MWE) that illustrates your problem. – cfr Dec 17 '14 at 2:28
  • thanks all - I got it working with what @HarishKumar said, though then I noticed there were some odd artifacts at the start of the doc, then I realised that the image wasn't going where I wanted so I had to add [!hbtp] to the function now it's just stopped working URGH. I'm probably going to be sorting this for a while now :( thanks though – baxx Dec 17 '14 at 2:36
  • @StevenB.Segletes I'll have a look at boxhandler thanks – baxx Dec 17 '14 at 2:36
3

This should be the right definition for your command \qweq:

\newcommand{\qweq}[4][!htbp]{%
\begin{figure}[#1]%
\centering%
\includegraphics[width=9cm]{/images/task#2/#3}%
\caption{\emph{\small{#4}}}%
\end{figure}%
}

Note, first of all, that each line is ended by % to avoid spurious spaces (especially in the caption...). Moreover a fourth optional argument (its default is !htbp) is given as the first argument to pass options to the figure environment.

When you're happy with the !htbp placement, you don't have to pass that argument, as in

\qweq{1}{donkey}{This is a donkey eating some grass. Nothing to do with databases but that's no problem.}

If you want to change it to, let's say !hb, you have to use it like

\qweq[!hb]{1}{donkey}{This is a donkey eating some grass again.}

MWE

\documentclass{article}
\usepackage{graphicx}
\usepackage{caption}
\captionsetup[figure]{justification=centering}

\newcommand{\qweq}[4][!htbp]{%
\begin{figure}[#1]%
\centering%
\includegraphics[width=9cm]{/images/task#2/#3}%
\caption{\emph{\small{#4}}}%
\end{figure}%
}

\begin{document}

\qweq{1}{donkey}{This is a donkey eating some grass. Nothing to do with databases but that's no problem.}

\qweq[!hb]{1}{donkey}{This is a donkey eating some grass again.}

\end{document} 

Output

enter image description here

Note the use of

\captionsetup[figure]{justification=centering}

for centering the caption, as suggested by Harish Kumar in his comment, to center the caption.

  • ace thanks @karlkoller, I was getting errors putting the function before begin{document} yesterday but they've stopped now, so all good. cheers :) – baxx Dec 17 '14 at 13:23
  • @user3130747 You're welcome. – karlkoeller Dec 17 '14 at 13:27

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.