# Tikz vertical lines next to variable text

I'm editing the caption of the Figure environment in a document using Tikz, but I don't know how to make the caption (variable width) to be next to two lines, something like this

I guess the Figure 1 part can be rotating a rectangle, but the caption text is more complicated since I don't know how to draw a single side, and also an extra line with the same height.

I'm also using this post I made before: Modify Figure environment

• Please give us (as a small, compilable document) the code which you are using to edit the caption already. Otherwise, any answer is likely to be a poor fit, at best.
– cfr
Jun 27, 2015 at 0:20
• Sorry, I have should done that. I also need to figure out how to avoid the centering so the node is to the left Jun 27, 2015 at 0:52
• You should better add your solution as an answer. (You can also accept your own answer but the system might make you wait to do that. I'm not sure.)
– cfr
Jun 27, 2015 at 23:31
• It's ok, you did most of the job. I added a separate answer as suggested Jun 27, 2015 at 23:38
• Thanks. I added another version although I don't like it. Your code is neater even if TeX does complain about overfull boxes.
– cfr
Jun 28, 2015 at 0:39

Something like this? I'm not entirely sure how you envisage this working but I'd recommend using the facilities of caption rather than trying to create your own stuff from scratch, given that you are loading caption already.

\documentclass{article}
\usepackage{caption,array}
\usepackage{lipsum}
\usepackage{tikz}
\usetikzlibrary{positioning}

\DeclareCaptionFormat{sidewise}{%
\begin{tikzpicture}
\node (caption text) [align=left] {%
\begin{tabular}{p{60mm}}
#3
\end{tabular}};
\node (caption label) [rotate=90, inner xsep=0pt, left=5mm of caption text.north west, font=\sffamily] {#1};
\draw [ultra thick] (caption label.south west) -- (caption label.south east);
\draw [ultra thick, orange!75!black]  ([xshift=.35em]caption label.south west) -- ([xshift=.35em]caption label.south east);
\end{tikzpicture}}
\captionsetup{format=sidewise}
\begin{document}
\lipsum[1]

\begin{figure}[t!p]
\centering
\includegraphics[scale=.5]{example-image-a}
\caption{My caption can be of a variable size.}
\label{fig:mylabel}
\end{figure}%

\lipsum[2]

\begin{figure}
\centering
\includegraphics[scale=.5]{example-image-b}
\caption{{This will be a much longer caption. Hence, we need another line.}}
\label{fig:myotherlabel}
\end{figure}

\end{document}


# EDIT

This is a modification of the version you posted building on my answer above. I wanted to avoid the overfull box warnings that was generating by using an overlay TikZ picture. What I don't understand is why I cannot use a minipage environment of width \textwidth. But it does avoid the warnings, although I'm not sure it is the most efficient method.

Note that you need two runs for things to end up in the right places. On first compilation, things will be (apparently) randomly scattered all over the place.

\documentclass{article}
\usepackage{caption,array}
\usepackage{lipsum}
\usepackage{tikz,tikzpagenodes}

\usetikzlibrary{positioning,tikzmark,calc}

\DeclareCaptionFormat{sidewise}{%
\raggedleft%
\begin{tikzpicture}[remember picture]%
\node (caption text) [align=left] {%
\begin{minipage}{.975\textwidth}
#3
\end{minipage}};
\end{tikzpicture}%
\begin{tikzpicture}[overlay, remember picture]
\node (caption label) [rotate=90, inner xsep=0pt, left=5mm of caption text.north west, font=\sffamily] {#1};
\draw [ultra thick] (caption label.south west) -- (caption label.south east);
\draw [ultra thick, orange]  ([xshift=.35em]caption label.south west) -- ([xshift=.35em]caption label.south east);
\end{tikzpicture}}
\DeclareCaptionLabelSeparator{none}{}
\captionsetup{format=sidewise,justification=justified}

\begin{document}

\begin{figure}
\centering
\includegraphics[scale=.5]{example-image-b}
\caption{{This will be a much longer caption. Hence, we need another long long long long long long long long long long long long line.}}
\label{fig:myotherlabel}
\end{figure}

\lipsum[1]

\end{document}


• Thank you very much, it's a great solution. I was playing a little with it and, what if I wanted to shift the Figure 1 (for example) more to the left? I tried adding: \node (caption text) [align=left, xshift=-2cm] {% \begin{tabular}{p{130mm}} #3 \end{tabular}}; but nothing happens, maybe the caption does not allow me to move the Tikz figure beyond a specific box. I was trying to move it a little behind the margin. Jun 27, 2015 at 16:48
• @DavidWinchester Glad you like it, but I'm not sure I quite understand the question. If I change p{60mm} to p{130mm}, then the caption etc. starts somewhat to the left of the image. Is that what you mean? Otherwise, maybe you want to switch off the \centering within the figure environment?
– cfr
Jun 27, 2015 at 20:34
• Yes, you are right, but it won't go further the left margin, it is like restricted inside a box. Maybe there is an option in the caption package. I was thinking something like: --- Figure caption-- | Figure text where | is the left margin. Sorry if I am not that clear. I will look into the documentation. Jun 27, 2015 at 21:33
• @DavidWinchester That's a bit different. Can you make a mock-up which shows how you want it to look in relation to the text below the figure (where the figure is placed on top of the page) or in relation to the footer or whatever (as appropriate)? Might be best to ask a new question and link this one. I'm thinking you can do it with page nodes and an overlay, maybe with a 'tikzmark'. But it makes a difference how it should affect stuff beneath it. Should vertical space be inserted for the 'Figure 1' or should vertical spacing ignore the rotated labels?
– cfr
Jun 27, 2015 at 21:44
• @DavidWinchester Great! See also my edit above. Although I'm sure it must be possible to avoid the warnings and keep the code a bit neater, at least....
– cfr
Jun 28, 2015 at 0:37

Using the previous answer, I modified it to get what I was thinking:

\documentclass{article}
\usepackage{caption,array}
\usepackage{lipsum}
\usepackage{tikz}

\usetikzlibrary{positioning}

\DeclareCaptionFormat{sidewise}{%
\hspace{-1.3cm}%
\begin{tikzpicture}
\node (caption text) [align=left] {%
\begin{tabular}{p{\textwidth}}
#3
\end{tabular}};
\node (caption label) [rotate=90, inner xsep=0pt, left=5mm of caption text.north west, font=\sffamily] {#1};
\draw [ultra thick] (caption text.north west) -- ([yshift=.95em]caption text.south west);
\draw [ultra thick, orange]  ([xshift=.35em]caption text.north west) -- ([xshift=.35em, yshift=.95em]caption text.south west);
\end{tikzpicture}}
\captionsetup{format=sidewise}

\begin{document}

\begin{figure}
\centering
\includegraphics[scale=.5]{example-image-b}
\caption{{This will be a much longer caption. Hence, we need another long long long long long long long long long long long long line.}}
\label{fig:myotherlabel}
\end{figure}

\lipsum[1]

\end{document}