调整 pgfplot 的大小,使其适合多列

调整 pgfplot 的大小,使其适合多列

我对 TeX 还很陌生,但我正在尝试整理一个multicol布局中列内有内联图表的文档。图表似乎总是超出列的宽度,我不知道如何正确缩放它。

以下是标记和结果文档:

\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}

在此处输入图片描述

作为一个相关问题,如果我想要 2 个并排的图表跨越整个页面宽度,那么最好的方法是什么?

答案1

您可以将整个图形缩放到列宽,例如

\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}

在此处输入图片描述

答案2

您可以简单地使用\resizebox\noindent这是基本的)。还请注意%后面的\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} 

在此处输入图片描述

答案3

将宽度固定为 ,0.95\columnwidth并将yticklabels 修改为

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

我们得到

在此处输入图片描述

代码:

\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}

不旋转标签:

\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}

在此处输入图片描述

并且不减小 ytick 标签的字体大小:

\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}

在此处输入图片描述

相关内容