I'm trying to draw a textbox over an image like this:

enter image description here

Here's what I have so far:

\documentclass{article}
\usepackage[paperwidth=50cm,paperheight=70cm,margin=1.3cm]{geometry}
\usepackage{xcolor}
\usepackage{tikz}

\definecolor{background}{HTML}{2C414C}
\definecolor{foreground}{HTML}{FFFFFF}

\begin{document}
\pagecolor{foreground}

\begin{center}
  \begin{tikzpicture}[y=0.80pt, x=0.80pt, yscale=-1, xscale=1, inner sep=0pt, outer sep=0pt]
    % IMAGE
    \node[anchor=north west,inner sep=0] at (0,0) {\includegraphics[scale=3.0]{example-image-c}};
    % OUTER RECTANGLE
    \path[draw=foreground,fill=background,line width=1.600pt]
      (232,465) rectangle (512,586);
    % INNER RECTANGLE
    \path[draw=foreground,fill=background,line width=0.800pt]
      (241,476) rectangle (502,576);
    % TITLE
    \path[fill=foreground] (340.3773,522.9204) node[above right] (text3826) {TITLE};
    % SUBTITLE
    \path[fill=foreground] (331.4183,546.7606) node[above right] (text3830) {SUBTITLE};
  \end{tikzpicture}
\end{center}

\end{document}

How do I:

  1. Horizontally center OUTER RECTANGE within IMAGE.
  2. Center INNER RECTANGLE within OUTER RECTANGLE.
  3. Center TITLE and SUBTITLE within INNER RECTANGLE as shown.

I don't want to use precise coordinates like (241,476) all over the place. Any alternative ways of drawing this (even without TikZ) would be welcome!

  • Please can you make your code compilable and use an image we all have, such as example-image or example-image-c or whatever? – cfr Aug 19 at 11:54
  • @cfr: done, thanks for the suggestion! – kontextify Aug 19 at 12:01
  • @cfr's proposal is better, but if you really need to put the boxes at these coordinates, just put them at the center of the coordinates and assign them the necessary minimum width and minimum height. – marmot Aug 19 at 15:58
up vote 6 down vote accepted

I'd name the node with the image and then place the other things relative to that, using positioning and fit. The picture then consists of three nodes: the picture, the box with text and a box with the thick outer line.

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\definecolor{background}{HTML}{2C414C}
\definecolor{foreground}{HTML}{FFFFFF}
\usetikzlibrary{positioning,fit}
\begin{document}
\begin{tikzpicture}[inner sep=0pt, outer sep=0pt, text=foreground, draw=foreground, fill=background, font=\sffamily\LARGE]
  \node (n) {\includegraphics{example-image}};
  \node (o) [draw, fill, double distance=2.5ex, double=background, line width=.8pt, inner ysep=20pt, inner xsep=50pt, anchor=south, above=5ex of n.south, align=center] {TITLE\\SUBTITLE};
  \node [draw, line width=1.6pt, fit=(o), inner sep=1.25ex] {};
\end{tikzpicture}
\end{document}

nodes in layers

  • Thanks, positioning and fit are exactly what is needed here! – kontextify Aug 20 at 8:07

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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