7

tl;dr How do I align text to text within \included graphics?

Let's look at the following logo, which I want to include (in a vector format, e.g. pdf or svg). enter image description here

The blue/red lines are my annotations.

While the logo is a vector image, it is an external image. How do I let LaTeX know where the 'baseline' is? Or where, in this image, to left-align text to? See red/blue lines respectively.

Most of my LaTeX documents are rather hack-ish at the moment, I am trying to find an elegant solution for this.

Things I've considered:

  • Meddle with the bounding box. What are the drawbacks?
  • Manually position the logo. To avoid resizing issues, I'd have to find (e.g. baseline) as relative height.
  • I have little knowledge of PDF as a language, but is there a way to tell LaTeX, e.g. "go use the bottom left corner of element ", since I am including vector graphics?
  • Maybe I can include the graphic with tikz, and then manually position the anchor?

I would appreciate any suggestions you guys might have.

  • Since the external image is self-contained, there is no reference to whether it includes text or "just graphics" - they're all the same. Also, by default, the baseline is set as the bottom of the image. Sure you can manipulate that, but the default is to have no depth, putting the bottom of the image on the baseline. – Werner Aug 29 '13 at 19:19
  • @Werner, if I were to convert the vector drawing to pgf code, would I then be able to do so? It would still be 'just graphics', but maybe I could reference pgf nodes? – kadrach Aug 29 '13 at 19:28
  • Maybe (I'm not familiar with SVG to PGF conversion in Inkscape, say). You may end up with separate nodes (or something) for each element in the original SVG which, again, has to technical reference to text or graphics. I guess it depends heavily on the source. – Werner Aug 29 '13 at 19:31
5

I'm not sure what tools are out there for changing the bounding boxes of images, but it can be done in LaTeX, as shown here. The primary tools I used are:

1) the \addvbuffer command of the verbatimbox package for vertically padding a box with positive or negative padding; and

2) the \stackengine command from the package of the same name. When the 7th parameter (known as \useanchorwidth) is set true, the total stack takes the horizontal width of the stack's anchor object.

I just made sure that the anchors were rules of the desired sizes. There are four parameters to set, shown near the top of the MWE.

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

\def\topclip{-.76cm}
\def\bottomclip{-.76cm}
\def\finalwidth{4.5cm}
\def\widthplusrightclip{6cm}

\def\wwfraw{\includegraphics{wwf}}

\def\wwf{\addvbuffer[\topclip\ \bottomclip]{%
  \stackengine{0pt}{\rule{\finalwidth}{0cm}}{%
    \stackengine{0pt}{\rule{\widthplusrightclip}{0cm}}{\wwfraw}{O}{r}{F}{T}{L}%
  }{O}{l}{F}{T}{L}%
}}%

\def\cornermark{\color{red}\rule{0.5cm}{0.5cm}}
\parindent 0in
\begin{document}
\def\stackalignment{r}\topinset{\cornermark}{%
  \def\stackalignment{l}\bottominset{\cornermark}{\wwfraw}{}{}}{}{}
\clearpage
Left margin\vspace{1in}\par 
\def\stackalignment{r}\topinset{\cornermark}{%
  \def\stackalignment{l}\bottominset{\cornermark}{\wwf}{}{}}{}{}
Here is the baseline
\clearpage
\def\stackalignment{l}
\bottominset{\color{red}\Huge \textsc{My Text}}{\wwfraw}{6cm}{1.5cm}
\end{document}

Here are the bounds of the raw image, as downloaded. The red boxes signify the (inclusive) extent of the lower left and upper right of the image

enter image description here

After processing, here is the image extent, again shown with red boxes:

enter image description here

Once you know the extent of the image, you can plan your text accordingly.

Of course, unless you feel compelled to set an origin to the image, you can place your text anywhere even on the raw image. For example, the last few lines of the MWE:

\def\stackalignment{l}
\bottominset{\color{red}\Huge \textsc{My Text}}{\wwfraw}{6cm}{1.5cm}

will produce the following, without any need to do the bounding-box alteration.

enter image description here

Of course, if you choose to operate on the processed image, then setting the inset text with a (0,0) offset from the lower left:

\bottominset{\color{red}\Huge \textsc{My Text}}{\wwf}{}{}

will place the lower left of the text on the blue/red registration marks.

4

Although in theory if you have the image in a vector format it may be possible to calculate the baseline, other issues usually mean you have to manually tweak it anyway, so unless you have to do thousands of such images, just adjusting the position by eye is usually sufficient. Really you don't need any additional packages for that, just \raisebox and \kern (or \hspace).

enter image description here

\documentclass{article}

\usepackage{fix-cm}
\usepackage{graphicx}


\begin{document}


\fontfamily{cmss}\fontseries{bx}\fontsize{45}{50}\selectfont



\raisebox{-.07\height}{\includegraphics{wwf}}%
\kern-42pt WWF




\end{document}
  • Comparing our two answers, it is worth noting that \raisebox and \addvbuffer are not the same. With \raisebox, LaTeX knows that the negative part of the image still extends below the baseline. Thus, unless I'm mistaken, sticking it in a bottom-aligned float, for example, won't push the new baseline against the page base. Am I mistaken? As far as horizontal clip, of course the kern is easier...stupid me. – Steven B. Segletes Aug 29 '13 at 20:32
  • @StevenB.Segletes normally (out of the figure box) latex lines the baseline against the page botton unless the depth is too big (\maxdepth) in which case it moves it up, you can of course \smash the depth but then by default it overprints following text, so it all depends what's needed.... – David Carlisle Aug 29 '13 at 20:37
  • With the kern, you fixed the right margin of the figure. However, because you can't kern to the left of the left margin, you can't use \kern to move the vertical blue registration line to the left margin, best as I can tell. – Steven B. Segletes Aug 29 '13 at 20:55
  • @StevenB.Segletes just put a -ve kern before the image include. – David Carlisle Aug 29 '13 at 20:58
  • Ooo, that's new to me. Thanks for the pointer. – Steven B. Segletes Aug 29 '13 at 21:05

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.