5

I have this figure (/ 2 figures)

\documentclass{article}
\usepackage{pgfplots}
\usepackage{subcaption}

\newcommand{\xfigscale}{.5}
\newcommand{\yaxscale}{.75}

\begin{document}

\begin{figure}
     \begin{subfigure}[b]{\xfigscale\textwidth}
          \centering
\resizebox{\linewidth}{!}{
          \begin{tikzpicture}
          \begin{axis}[
            scale only axis, 
            height=\yaxscale\textwidth,
            axis lines=middle,
            axis line style={->},
            x label style={at={(axis description cs:0.5,-0.1)},anchor=north},
            y label style={at={(axis description cs:-0.1,.5)},rotate=90,anchor=south},
            xlabel={X},
            ylabel={Y}]
          \addplot[black,samples=100,domain=0:100] {x};
          \end{axis}
          \end{tikzpicture}
}
          \caption{a}
          \label{fig:a}
     \end{subfigure}

     \begin{subfigure}[b]{\xfigscale\textwidth}
          \centering
          \resizebox{\linewidth}{!}{
          \begin{tikzpicture}
          \begin{axis}[
            scale only axis, 
            height=\yaxscale\textwidth,
            axis lines=middle,
            axis line style={->},
            x label style={at={(axis description cs:0.5,-0.1)},anchor=north},
            y label style={at={(axis description cs:-0.1,.5)},rotate=90,anchor=south},
            xlabel={X},
            ylabel={Y}]
          \addplot[black,samples=100,domain=0:100] {sqrt(x)};
          \end{axis}
          \end{tikzpicture}
}
          \caption{b}
          \label{fig:b}
     \end{subfigure}
\caption{cap}
 \end{figure}

\end{document}

Since there are some of these and only the y label and the function changes, I thought it might be possible to write a command to "add" the rest, i. e. the things which do not change, but I dont know how. This is what I tried:

    \newenvironment{splot}[1]{
    \begin{tikzpicture}
    \begin{axis}[scale only axis,
    height=\yaxscale\textwidth,
    axis lines=middle,
    axis line style={->},
    x label style={at={(axis description cs:0.5,-0.1)},anchor=north},
    y label style={at={(axis description cs:-0.1,.5)},rotate=90,anchor=south},
    xlabel={X},
    ylabel={#1}]}
    {\end{axis}\end{tikzpicture}}

    \newcommand{plotfun}[1]{\addplot[
    black,samples=100,domain=0:100]{#1};}

\begin{document}

    \begin{splot}[$Y label$]
    \plotfun{sqrt(x)}
    \end{splot}

\end{document}

but apparently \addplot[black,samples=100,domain=0:100]{#1}; cannot be put into a command like that.

Also is it possible to have both figures side by side. All the time they are in a column and not in a row, even though I tried to shrink them already...?

Update: So I took David's hint and skipped defining a new addplot which is probably more difficult. The following code compiles now, but the figures just dont stay side by side. Why so?

\begin{figure}
     \begin{subfigure}[b]{\xfigscale\textwidth}
          \centering
\resizebox{\linewidth}{!}{
   \begin{splot}{$Y label$}
    \addplot[black,samples=100,domain=0:100]{x};
    \end{splot}
    }
          \caption{a}
          \label{fig:a}
     \end{subfigure}

     \begin{subfigure}[b]{\xfigscale\textwidth}
          \centering
          \resizebox{\linewidth}{!}{
   \begin{splot}{$Y label$}
    \addplot[black,samples=100,domain=0:100]{sqrt(x)};
    \end{splot}
    }
          \caption{b}
          \label{fig:b}
     \end{subfigure}
\caption{cap}
 \end{figure}
  • Ypu have defined splot to take one mandatory argument so it needs {} not [] around that argument. – David Carlisle Nov 14 '14 at 17:34
9
  1. Using Styles (Recommend Approach):

    You should use tikzset to define your own styles. Below, I defined My Axis Style for your axis style, and My Plot Style for your plot:

    enter image description here

    Notes:

    • Removing the blank line between the two sub figures will put the figures next to each other. This is the similar to the behavior in normal text paragraphs where a blank line indicated the start of a new paragraph.

  1. Custom Environment:

    To main problem with the custom environment that you were trying to define was the line

    \newcommand{plotfun}{...}
    

    It needed to be

     \newcommand{\plotfun}{...}
    

    The alternative solution below shows you how to have a custom environment and provides an optional parameter to splot and \plotfun that allows further customization. For illustative purposes, I used the optional parameter to splot to alter the axis style of the first plot, and the optional parameter to \plotfun to alter the line style:

    enter image description here


Code: Using styles:

\documentclass{article}

\usepackage{pgfplots}
\usepackage{subcaption}

\newcommand{\xfigscale}{.5}
\newcommand{\yaxscale}{.75}

\tikzset{/pgfplots/My Axis Style/.style={
            scale only axis, 
            height=\yaxscale\textwidth,
            axis lines=middle,
            axis line style={->},
            x label style={at={(axis description cs:0.5,-0.1)},anchor=north},
            y label style={at={(axis description cs:-0.1,.5)},rotate=90,anchor=south},
            xlabel={X},
            ylabel={Y},
    }
}

\tikzset{My Plot Style/.style={black,samples=100,domain=0:100, ultra thick}}


\begin{document}

\begin{figure}
     \begin{subfigure}[b]{\xfigscale\textwidth}
          \centering
          \resizebox{\linewidth}{!}{% <--- Note
          \begin{tikzpicture}
          \begin{axis}[My Axis Style]
              \addplot[My Plot Style, blue] {x};
          \end{axis}
          \end{tikzpicture}%
           }
          \caption{a}
          \label{fig:a}
     \end{subfigure}%   <--- Note
    %                   <--- Note
     \begin{subfigure}[b]{\xfigscale\textwidth}
          \centering
          \resizebox{\linewidth}{!}{% <--- Note
          \begin{tikzpicture}
          \begin{axis}[My Axis Style]
              \addplot[My Plot Style, red] {sqrt(x)};
          \end{axis}
          \end{tikzpicture}%  <--- Note
          }
          \caption{b}
          \label{fig:b}
     \end{subfigure}
    \caption{cap}
 \end{figure}
\end{document}

Code: Custom Environment

\documentclass{article}

\usepackage{pgfplots}
\usepackage{subcaption}

\newcommand{\xfigscale}{.5}
\newcommand{\yaxscale}{.75}

\tikzset{/pgfplots/My Axis Style/.style={
            scale only axis, 
            height=\yaxscale\textwidth,
            axis lines=middle,
            axis line style={->},
            x label style={at={(axis description cs:0.5,-0.1)},anchor=north},
            y label style={at={(axis description cs:-0.1,.5)},rotate=90,anchor=south},
            xlabel={X},
            ylabel={Y},
    }
}

\tikzset{My Plot Style/.style={black,samples=100,domain=0:100, ultra thick}}


\newenvironment{splot}[2][]{
    \begin{tikzpicture}
    \begin{axis}[scale only axis,
    height=\yaxscale\textwidth,
    axis lines=middle,
    axis line style={->},
    x label style={at={(axis description cs:0.5,-0.1)},anchor=north},
    y label style={at={(axis description cs:-0.1,.5)},rotate=90,anchor=south},
    xlabel={X},
    ylabel={#2},
    #1
    ]
}{%
    \end{axis}\end{tikzpicture}%
}

%% Note The command name is "\plotfun" not "plotfun"
\newcommand{\plotfun}[2][]{\addplot[My Plot Style,#1]{#2};}


\begin{document}


\begin{figure}
     \begin{subfigure}[b]{\xfigscale\textwidth}
          \centering
          \resizebox{\linewidth}{!}{%
            \begin{splot}[axis line style={<->, magenta},]{$Y_1$}
                \plotfun[blue]{x}
            \end{splot}%
           }
          \caption{$y=x$}
          \label{fig:a}
     \end{subfigure}%   <--- Note
    %                   <--- Note
     \begin{subfigure}[b]{\xfigscale\textwidth}
          \centering
          \resizebox{\linewidth}{!}{%
            \begin{splot}{$Y_2$}
                \plotfun[red]{sqrt(x)}
            \end{splot}%
          }
          \caption{$y=\sqrt{x}$}
          \label{fig:b}
     \end{subfigure}
    \caption{Twp plots via custom environment.}
 \end{figure}
\end{document}
|improve this answer|||||
  • so this is how it works. But the addplot command cannot be put in another one, can it? Has it to do with optional arguments? – embert Nov 14 '14 at 17:57
  • Why is the ` %` neccessary? Is it what keeps the figures side by side? – embert Nov 14 '14 at 17:59
  • @embert: Defined a new style for your plot as well, and added not regarding the %. Using styles is preferable to defining your environment as I believe that that will have issues if you try to use the externalization library. – Peter Grill Nov 14 '14 at 18:06
  • @embert: Provided a custom environment solution as well. Also, found another missing % that should have been there in the first solution as well. – Peter Grill Nov 14 '14 at 19:06

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.