5

Problem:

I have searched how you can align text without the use of tables using LaTex. What I am after is to align images after each other on one side and text to align to respective image on the right hand side.

Example of problem:

enter image description here

Desired outcome:

Basically I am after to be able to align a photo and corresponding text exactly aligned horizontally to the middle of that photo. Each photo/text part should be divided by an arrow which is exactly aligned vertically in the middle of each photo.

I would appreciate if someone could give advice what the best practice is without using any tables.

  • The floatrow package? The text may be a beside caption (without any label) – Bernard Aug 29 '15 at 18:38
  • 2
    Why don't you want to use tables? – Alan Munn Aug 29 '15 at 18:51
  • @AlanMunn I'm using a template and the tables are already pre-formatted in a certain way that it would not be suitable to reproduce the example I posted. The tables use tabu - if there is a way to use tables without tabu then I am willing to give it a try. Any suggestions? – kexxcream Aug 29 '15 at 18:53
  • It is fine to use tabu provided you will not be concerned when your documents break. The author has promised an update with no backwards compatibility and no support for existing code. Meanwhile, serious bugs will not be fixed. – cfr Aug 31 '15 at 0:51
8

The tabular environment really is the simplest way to do this. Here's a solution using regular tabular. You can use it alongside the tabu package without any problem.

Arrows are taken from Fancy arrows with TikZ

Compatibility with siunitx

The cellspace package is used here to get the vertical spacing around the images to be nice. cellspace defines an S specifier before a column specification. If you are using siunitx, which also defines an S column, you need to change the cellspace column specifier to C. (siunitx handles this automatically, by creating the appropriate C column specifier, but you need to change your code in the tabular.)

Code

\documentclass[svgnames]{article}
\usepackage{tabu} % just to show compatibility
\usepackage{array}
\usepackage[demo]{graphicx}
\usepackage{tikz}
\usetikzlibrary{fadings,shapes.arrows,shadows}

\usepackage{cellspace, graphicx}
\renewcommand\cellspacetoplimit{3pt}
\renewcommand\cellspacebottomlimit{3pt}
\usepackage{xparse}


\tikzfading[name=arrowfading, 
            top color=transparent!0, bottom color=transparent!95]
\tikzset{arrowfill/.style={#1,general 
    shadow={fill=black, shadow yshift=-0.8ex, path fading=arrowfading}}}
\tikzset{arrowstyle/.style n args={3}{draw=#2,arrowfill={#3}, 
    single arrow,minimum height=#1, single arrow,
    single arrow head extend=.3cm,}}

\NewDocumentCommand{\tikzfancyarrow}
     {O{2cm} O{FireBrick} O{top color=OrangeRed!20, bottom color=Red} m}{
     \tikz[baseline=-0.5ex]\node [arrowstyle={#1}{#2}{#3}] {#4};
}
\newcommand{\myarrow}{
   \tikzfancyarrow[1cm][GreenYellow][top color=DarkSeaGreen, bottom
    color=GreenYellow,rotate=-90]{}}

\begin{document}

% If you are using siunitx, change S to C in this tabular
% 
\begin{tabular}{S{>{\centering}m{1.25in}}S{m{4in}}}
\includegraphics[width=1in]{foo} & This is some text that describes the
 first figure. It will take up as much space as is needed and will remain
 centred I hope.\\
\myarrow & \\
\includegraphics[width=1in,height=2in]{foo} & This is some text that
 describes the second figure. It will take up as much space as is needed 
 and will remain centred. As the text gets longer  it centres in the way 
 that we want. So it seems that this is working.\\
\myarrow & \\
\includegraphics[width=1in,height=.5in]{foo} & This is the text on the last 
image.
\end{tabular}
\end{document}

enter image description here

  • Doesn't seem to work in my template, I only get a lot of errors. I finally narrowed it down to S{>{\centering}m{1.25in}}S{m{4in}} - when I change this to cc then the errors disappear but the figure doesn't look good. Why is it causing errors? – kexxcream Aug 30 '15 at 7:31
  • @kexxcream Without knowing anything about you "template" it's really hard to tell. The way to diagnose this is to take just my code, put it into a document which uses your template and then see if it works. If it doesn't then start commenting out packages in the template until the document compiles. This will tell you if there's a package conflict. Are you also using siunitx? – Alan Munn Aug 30 '15 at 15:25
2

This solution does not rely on tables. Whether it will work with your template, however: that is anyone's guess. As Alan Munn pointed out, we cannot possibly know.

I define two new commands:

  • \imgfraction{<fraction>} takes a single argument which specifies the fraction of the text width to be occupied by the images e.g \imgfraction{.75} or \imgfraction{.002};

  • \imgtxt[<options passed to \includegraphics>]{<image filename>}{<text>} takes one optional and two mandatory arguments, where the first specifies options to pass to \includegraphics, the second specifies the filename of the image, and the third specifies the text e.g. \imgtxt[width=.75\linewidth]{example-image-a}{Great explanation of A's importance} or \imgtxt{small-image}{Questions raised}.

TikZ is used to draw the arrow. This is saved in a box for re-use to avoid the need to repeatedly render it.

The command is configured so that it will generally do the right thing in terms of whether to use an arrow or not. It can, for example, cope with more than one image sequence in a document (using afterpage). As I understand your use-case, however, I take it that you do not need sequences which themselves span multiple pages and the code is not designed to cope with that kind of eventuality.

Here's an example of two sequences one using example images from the mwe package and the other using the ubiquitous tiger supplied in standard distributions. Note that \imgfraction{} is used in between to adjust the proportion of the page used for the tigers in the second sequence.

image sequences

\documentclass{article}
\usepackage[x11names]{xcolor}
\usepackage{calc,tikz,afterpage,kantlipsum}
\usetikzlibrary{shapes.arrows,shadows}
\newcommand\imgtxt[3][]{%
  \iffirstimgtxt\firstimgtxtfalse\afterpage{\global\firstimgtxttrue}%
  \else
    \noindent\begin{minipage}{\imgtxtimgwidth}
      \centering
      \usebox\imgtxtarrow
      \medskip
      \par
    \end{minipage}
    \par
  \fi
  \noindent
  \begin{minipage}[c]{\imgtxtimgwidth}
    \centering\includegraphics[#1]{#2}\medskip\par
  \end{minipage}%
  \begin{minipage}[c]{\imgtxttxtwidth}
    #3\medskip\par
  \end{minipage}\par}
\newif\iffirstimgtxt
\firstimgtxttrue
\newsavebox\imgtxtarrow
\sbox\imgtxtarrow{%
  \tikz{%
    \node [single arrow, drop shadow, draw=Green4, line width=1pt, inner color=white!50!Green4, outer color=white!10!Green4, shape border uses incircle, shape border rotate=-90, text height=2.5mm, text width=5mm]  {};
  }%
}
\newlength\imgtxtimgwidth
\newlength\imgtxttxtwidth
\newcommand*\imgfraction[1]{%
  \setlength\imgtxtimgwidth{#1\textwidth}%
  \setlength\imgtxttxtwidth{\textwidth-#1\textwidth}%
}
\imgfraction{.25}
\begin{document}
  \imgtxt[width=.75\linewidth]{example-image-a}{\kant[1]}
  \imgtxt[width=.75\linewidth]{example-image-b}{A very little text.}
  \imgtxt[width=.75\linewidth]{example-image-c}{\kant[2]}

  \newpage\imgfraction{.3}

  \imgtxt[width=.9\linewidth]{tiger}{Introducing tigers and other big cats.}
  \imgtxt[width=.9\linewidth]{tiger}{A very little more text.}
  \imgtxt[width=.9\linewidth]{tiger}{\kant[3]}

\end{document}

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.