自动将附录中的彩色 PGFlots 转换为灰度

自动将附录中的彩色 PGFlots 转换为灰度

我的文档包含大量彩色 PGFPlot,这些 PGFPlot 是从外部数据文件生成的。为了节省打印成本,我想自动将附录中每个 PGFPlot 的颜色转换为灰度,这样我就可以在正文部分和附录的图形之间切换,颜色会自动调整。

我找到了几篇关于如何自动将 PDF 或包含的图形转换为灰度的帖子,但遗憾的是没有关于如何使用 PGFPlots 执行相同操作的信息。为了说明我的意思,我准备了一个相当长的 MWE,它与我的真实代码非常接近:

麦格

\documentclass[12pt,a4paper]{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage[usenames,dvipsnames,svgnames,x11names]{xcolor}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\pgfplotsset{compat=1.9}

\usepackage{adjustbox}
\usepackage{float}
\floatstyle{boxed}
\restylefloat{figure}
\floatplacement{figure}{htb!}
\usepackage{placeins}

\usepackage{lipsum}

\begin{document}
    \section{Introduction}
    \lipsum[1]

    \section{Body}
    \lipsum[2]

    \begin{figure}
        \begin{adjustbox}{max width=\textwidth,max height=\textheight}
            \begin{tikzpicture}
                \begin{groupplot}[ybar=0pt,/pgf/bar width=1,ymin=0,xmin=0.5,xmax=5.5,width=\textwidth,group style={group size=1 by 2,xticklabels at=edge bottom,yticklabels at=edge left,vertical sep=18pt},ylabel style={rotate=-90,text width=1.8cm,align=right},axis x line*=left,point meta=explicit symbolic,every node near coord/.append style={font=\small},nodes near coords={\ifnum\pgfplotspointmeta>0 \pgfplotspointmeta\fi},cycle list={fill=gray!50,draw=black}]
                \nextgroupplot [ymax=14,y=1.5mm,try min ticks=2,ylabel={Foo's Bar'ed}]
                \addplot +[fill=green!50] table [y index=1, meta index=1] {mwe.dat};
                \nextgroupplot [ymax=14,y=1.5mm,try min ticks=2,ylabel={Bar's Foo'ed}]
                \addplot +[fill=orange!50] table [y index=2, meta index=2] {mwe.dat};
                \end{groupplot}
            \end{tikzpicture}
        \end{adjustbox}
        \caption{Colored Figure}
    \end{figure}

    \lipsum[3]
    \FloatBarrier

    \section{Appendix}
    \lipsum[4]

    \begin{figure}
        \begin{adjustbox}{max width=\textwidth,max height=\textheight}
            \begin{tikzpicture}
                \begin{groupplot}[ybar=0pt,/pgf/bar width=1,ymin=0,xmin=0.5,xmax=5.5,width=\textwidth,group style={group size=1 by 2,xticklabels at=edge bottom,yticklabels at=edge left,vertical sep=18pt},ylabel style={rotate=-90,text width=1.8cm,align=right},axis x line*=left,point meta=explicit symbolic,every node near coord/.append style={font=\small},nodes near coords={\ifnum\pgfplotspointmeta>0 \pgfplotspointmeta\fi},cycle list={fill=gray!50,draw=black}]
                \nextgroupplot [ymax=14,y=1.5mm,try min ticks=2,ylabel={Foo's Bar'ed}]
                \addplot +[fill=green!50] table [y index=1, meta index=1] {mwe.dat};
                \nextgroupplot [ymax=14,y=1.5mm,try min ticks=2,ylabel={Bar's Foo'ed}]
                \addplot +[fill=orange!50] table [y index=2, meta index=2] {mwe.dat};
                \end{groupplot}
            \end{tikzpicture}
        \end{adjustbox}
        \caption{Grayscale Figure}
    \end{figure}
\end{document}

数据表

Day FooBar BarFoo 
1   2      1      
2   1      0      
3   1      0      
4   3      1      
5   5      1        

MWE 第 1 页的截图 MWE 第 2 页截图

答案1

您可以\selectcolormodel{gray}在开始附录时简单地包含它。

代码(包含mwe.dat文件内容):

\documentclass[12pt,a4paper]{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage[usenames,dvipsnames,svgnames,x11names]{xcolor}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\pgfplotsset{compat=1.9}

\usepackage{adjustbox}
\usepackage{float}
\floatstyle{boxed}
\restylefloat{figure}
\floatplacement{figure}{htb!}
\usepackage{placeins}
\usepackage{filecontents}
\usepackage{lipsum}

\begin{filecontents*}{mwe.dat}
Day FooBar BarFoo 
1   2      1      
2   1      0      
3   1      0      
4   3      1      
5   5      1   
\end{filecontents*}

\begin{document}
    \section{Introduction}
    \lipsum[1]

    \section{Body}
    \lipsum[2]

    \begin{figure}
        \begin{adjustbox}{max width=\textwidth,max height=\textheight}
            \begin{tikzpicture}
                \begin{groupplot}[ybar=0pt,/pgf/bar width=1,ymin=0,xmin=0.5,xmax=5.5,width=\textwidth,group style={group size=1 by 2,xticklabels at=edge bottom,yticklabels at=edge left,vertical sep=18pt},ylabel style={rotate=-90,text width=1.8cm,align=right},axis x line*=left,point meta=explicit symbolic,every node near coord/.append style={font=\small},nodes near coords={\ifnum\pgfplotspointmeta>0 \pgfplotspointmeta\fi},cycle list={fill=gray!50,draw=black}]
                \nextgroupplot [ymax=14,y=1.5mm,try min ticks=2,ylabel={Foo's Bar'ed}]
                \addplot +[fill=green!50] table [y index=1, meta index=1] {mwe.dat};
                \nextgroupplot [ymax=14,y=1.5mm,try min ticks=2,ylabel={Bar's Foo'ed}]
                \addplot +[fill=orange!50] table [y index=2, meta index=2] {mwe.dat};
                \end{groupplot}
            \end{tikzpicture}
        \end{adjustbox}
        \caption{Colored Figure}
    \end{figure}

    \lipsum[3]
    \FloatBarrier

    \section{Appendix}
    \selectcolormodel{gray}
    \lipsum[4]

    \begin{figure}
        \begin{adjustbox}{max width=\textwidth,max height=\textheight}
            \begin{tikzpicture}
                \begin{groupplot}[ybar=0pt,/pgf/bar width=1,ymin=0,xmin=0.5,xmax=5.5,width=\textwidth,group style={group size=1 by 2,xticklabels at=edge bottom,yticklabels at=edge left,vertical sep=18pt},ylabel style={rotate=-90,text width=1.8cm,align=right},axis x line*=left,point meta=explicit symbolic,every node near coord/.append style={font=\small},nodes near coords={\ifnum\pgfplotspointmeta>0 \pgfplotspointmeta\fi},cycle list={fill=gray!50,draw=black}]
                \nextgroupplot [ymax=14,y=1.5mm,try min ticks=2,ylabel={Foo's Bar'ed}]
                \addplot +[fill=green!50] table [y index=1, meta index=1] {mwe.dat};
                \nextgroupplot [ymax=14,y=1.5mm,try min ticks=2,ylabel={Bar's Foo'ed}]
                \addplot +[fill=orange!50] table [y index=2, meta index=2] {mwe.dat};
                \end{groupplot}
            \end{tikzpicture}
        \end{adjustbox}
        \caption{Grayscale Figure}
    \end{figure}
\end{document}

这给出了输出

第一页 第二页

相关内容