4

I'm very new to TeX but I'm trying to put together a document that has a multicol layout with a chart inline in the column. The chart always seems to overflow the width of the column and I can't figure out how to scale it correctly.

Here is the markup and resultant document:

\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}

\usepackage[margin=2cm]{geometry}
\usepackage{pgfplots}
\usepackage{lipsum}
\usepackage{multicol}

\begin{document}

\section*{Introduction}
\lipsum[1]


\begin{multicols}{2}
\section*{Risk and Reward Profile}
\lipsum[2-4]

\begin{tikzpicture}
    \begin{axis}[
        symbolic y coords={Equities, Absolute Return, Fixed Income, Property, Cash},
        ytick=data
      ]
        \addplot[xbar,fill=blue] coordinates {
            (51.3,Equities) (32.1,Absolute Return) (10.2,Fixed Income) (4.0,Property) (2.5,Cash)
        };
    \end{axis}
\end{tikzpicture}

\lipsum[7]
\end{multicols}
\end{document}

enter image description here

As a related question, if instead I wanted 2 charts side-by-side spanning the whole width of the page what is the best way to achieve this?

harryg
  • 185
  • 6
  • Welcome to TeX.SX! Please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem. – LaRiFaRi Mar 03 '15 at 13:30
  • 1
    Fair point. Question edited appropriately – harryg Mar 03 '15 at 13:32
  • Use the key setting width=2in or any other length you want in the axis environment options. – Paul Gessler Mar 03 '15 at 13:35

3 Answers3

4

You can simply use \resizebox (and \noindent which is fundamental). Also note the % after \end{tikzpicture}.

\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}

\usepackage[margin=2cm]{geometry}
\usepackage{pgfplots}
\usepackage{lipsum}
\usepackage{multicol}

\begin{document}

\section*{Introduction}
\lipsum[1]
A “balanced” risk strategy investing in a diversified range of asset classes with a return objective of UK cash deposits (LIBOR) + 3\% to 4\% p.a. over the medium term with target volatility of 6\%-8\%. The portfolio is designed to map to the ARC PCI Balanced and the STEP TMPI Medium Risk benchmarks, but at the lower end of the balanced risk range.


\begin{multicols}{2}
\section*{Risk and Reward Profile}
\lipsum[2-4]

\noindent\resizebox{\columnwidth}{!}{%
\begin{tikzpicture}
    \begin{axis}[
        symbolic y coords={Equities, Absolute Return, Fixed Income, Property, Cash},
        ytick=data
      ]
        \addplot[xbar,fill=blue] coordinates {
            (51.3,Equities) (32.1,Absolute Return) (10.2,Fixed Income) (4.0,Property) (2.5,Cash)
        };
    \end{axis}
\end{tikzpicture}%
}

\lipsum[7]
\end{multicols}
\end{document} 

enter image description here

karlkoeller
  • 124,410
2

After fixing the width to 0.95\columnwidth and tinkering yticklabels by

yticklabel style={inner sep=2pt,rotate=45,anchor=south east,font=\footnotesize}

we get

enter image description here

Code:

\documentclass{article}
\usepackage[utf8]{inputenc}

\usepackage[margin=2cm]{geometry}
\usepackage{pgfplots}
\usepackage{lipsum}
\usepackage{multicol}
\begin{document}

\section*{Introduction}
\lipsum[1]
A “balanced” risk strategy investing in a diversified range of asset classes with a return objective of UK cash deposits (LIBOR) + 3\% to 4\% p.a. over the medium term with target volatility of 6\%-8\%. The portfolio is designed to map to the ARC PCI Balanced and the STEP TMPI Medium Risk benchmarks, but at the lower end of the balanced risk range.


\begin{multicols}{2}
\section*{Risk and Reward Profile}
\lipsum[2-4]
\noindent
\begin{tikzpicture}
    \begin{axis}[
        width=0.95\columnwidth,
        symbolic y coords={Equities, Absolute Return, Fixed Income, Property, Cash},
        ytick=data,
        yticklabel style={inner sep=2pt,rotate=45,anchor=south east,font=\footnotesize}
      ]
        \addplot[xbar,fill=blue] coordinates {
            (51.3,Equities) (32.1,Absolute Return) (10.2,Fixed Income) (4.0,Property) (2.5,Cash)
        };
    \end{axis}
\end{tikzpicture}

\lipsum[7]
\end{multicols}
\end{document}

Without rotating the labels:

\documentclass{article}
\usepackage[utf8]{inputenc}

\usepackage[margin=2cm]{geometry}
\usepackage{pgfplots}
\usepackage{lipsum}
\usepackage{multicol}
\begin{document}

\section*{Introduction}
\lipsum[1]
A “balanced” risk strategy investing in a diversified range of asset classes with a return objective of UK cash deposits (LIBOR) + 3\% to 4\% p.a. over the medium term with target volatility of 6\%-8\%. The portfolio is designed to map to the ARC PCI Balanced and the STEP TMPI Medium Risk benchmarks, but at the lower end of the balanced risk range.


\begin{multicols}{2}
\section*{Risk and Reward Profile}
\lipsum[2-4]
\noindent
\begin{tikzpicture}
    \begin{axis}[
        width=0.9\columnwidth,
        symbolic y coords={Equities, Absolute Return, Fixed Income, Property, Cash},
        ytick=data,
        yticklabel style={font=\footnotesize}
      ]
        \addplot[xbar,fill=blue] coordinates {
            (51.3,Equities) (32.1,Absolute Return) (10.2,Fixed Income) (4.0,Property) (2.5,Cash)
        };
    \end{axis}
\end{tikzpicture}

\lipsum[7]
\end{multicols}
\end{document}

enter image description here

And without reducing the font size of ytick labels:

\documentclass{article}
\usepackage[utf8]{inputenc}

\usepackage[margin=2cm]{geometry}
\usepackage{pgfplots}
\usepackage{lipsum}
\usepackage{multicol}
\begin{document}

\section*{Introduction}
\lipsum[1]
A “balanced” risk strategy investing in a diversified range of asset classes with a return objective of UK cash deposits (LIBOR) + 3\% to 4\% p.a. over the medium term with target volatility of 6\%-8\%. The portfolio is designed to map to the ARC PCI Balanced and the STEP TMPI Medium Risk benchmarks, but at the lower end of the balanced risk range.


\begin{multicols}{2}
\section*{Risk and Reward Profile}
\lipsum[2-4]
\noindent
\begin{tikzpicture}
    \begin{axis}[
        width=\columnwidth,
        symbolic y coords={Equities, Absolute Return, Fixed Income, Property, Cash},
        ytick=data,
        yticklabel style={text width=0.15\columnwidth,align=right}
      ]
        \addplot[xbar,fill=blue] coordinates {
            (51.3,Equities) (32.1,Absolute Return) (10.2,Fixed Income) (4.0,Property) (2.5,Cash)
        };
    \end{axis}
\end{tikzpicture}

\lipsum[7]
\end{multicols}
\end{document}

enter image description here

  • Works well but I opted for @LaRiFaRi's answer as it didn't involve rotating the axis labels. Thanks all the same – harryg Mar 03 '15 at 13:44
  • @harryg Rotation of labels was not mandatory but just meant to show you an option. See the update. –  Mar 03 '15 at 14:05
2

You may scale the whole figure to column width like this:

\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}

\usepackage[margin=2cm]{geometry}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\usepackage{lipsum}
\usepackage{multicol}
\usepackage{csquotes}
\usepackage{siunitx}

\newsavebox{\measuredSize}
\newcommand{\resizeToWidth}[2]{%
    \pgfmathsetmacro{\pgfplotswidth}{#2}%
    \begin{lrbox}{\measuredSize}#1\end{lrbox}%
    \pgfmathsetmacro{\pgfplotswidth}{2*\pgfplotswidth-\wd\measuredSize}%
    #1%
}

\begin{document}
%
\section*{Introduction}
\lipsum[1]
A \enquote{balanced} risk strategy investing in a diversified range of asset classes with a return objective of UK cash deposits (LIBOR) + \SIrange{3}{4}{\percent} p.a.\ over the medium term with target volatility of \SIrange{6}{8}{\percent}. The portfolio is designed to map to the ARC PCI Balanced and the STEP TMPI Medium Risk benchmarks, but at the lower end of the balanced risk range.
%   
\begin{multicols}{2}
    \section*{Risk and Reward Profile}
    \lipsum[2-4]

\noindent   
\resizeToWidth{%
    \begin{tikzpicture}
    \begin{axis}[%
    ,yticklabel style={%
        ,text width=1.4cm % this is optional here... reduce as much as you like or leave it away
        ,align=right
        ,inner sep=0 % gets rid of the space around the labels
        ,xshift=-0.3em % puts some space back to the rith side of the labels. Adapt that to your needs
        }
    ,width=\pgfplotswidth
    ,symbolic y coords={Equities, Absolute Return, Fixed Income, Property, Cash}
    ,ytick=data
    ]
    \addplot[xbar,fill=blue] coordinates {
        (51.3,Equities) (32.1,Absolute Return) (10.2,Fixed Income) (4.0,Property) (2.5,Cash)
    };
    \end{axis}
    \end{tikzpicture}}{\columnwidth}

    \lipsum[7]
\end{multicols}
\end{document}

enter image description here

LaRiFaRi
  • 43,807