Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Consider the following simple PV-diagram:

http://i.imgur.com/U9nxbJU.png?1

The code below created it and the output is fine. I used kind of a workaround with an array to get the right tick labels on both of the axes. That works fine for low values, but if I want to set for instance V_f = 200, getting the tick label right is going to be a bit of a problem.

What's a proper fix to get the tick labels? Can I tell tikz to use the "next" element of my array every time it actually printed the tick? I could think of a solution which increments a counter only when the printing happens, but surely there's a tikz solution for this ?


\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\usetikzlibrary{arrows,arrows.meta, decorations.markings,backgrounds, intersections}
\makeatletter
\tikzset{
    nomorepostaction/.code=\makeatletter\let\tikz@postactions\pgfutil@empty, 
    my axis/.style={
        postaction={
            decoration={
                markings,
                mark=at position 1 with {
                    \arrow[ultra thick]{latex}
                }
            },
            decorate,
            nomorepostaction
        },
        thin,
        -, % switch off other arrow tips
        every path/.append style=my axis % this is necessary so it works both with "axis lines=left" and "axis lines=middle"
    }
}
\makeatother
\begin{document}
\begin{figure}
\def\xt{{0,"$V_i=1$",0,"$V_f=3$"}}\relax
\def\yt{{0,"$P_i=1$",0,"$P_f=3$"}}\relax
\begin{tikzpicture}
\begin{axis}[
    axis lines=left,
    width=.95\linewidth,
    axis line style={my axis},
    xmin = 0, xmax = 4,
    ymin = 0, ymax = 4,
    xticklabel={\pgfmathparse{\xt[\tick]}\pgfmathresult},
    xtick={1,3},
    yticklabel={\pgfmathparse{\yt[\tick]}\pgfmathresult},
    ytick={1,3},
    xlabel={Volume},
    ylabel={Pressure}
    ]
  \path[name path=axisa] (axis cs:\pgfkeysvalueof{/pgfplots/xmin},1) -- (axis cs:\pgfkeysvalueof{/pgfplots/xmax},1);
  \path[name path=axisb] (axis cs:\pgfkeysvalueof{/pgfplots/xmin},3) -- (axis cs:\pgfkeysvalueof{/pgfplots/xmax},3);
\addplot[name path=graph, black,very thick,  -latex ] coordinates
        {(1,1) (3,3)} 
        ;
   \path[name intersections={of=axisa and graph, by={interpa}}];
      \path[name intersections={of=axisb and graph, by={interpb}}];
   \draw[ultra thin, dashed] (axis cs:0,1) -- (interpa);
   \draw[ultra thin, dashed] (axis cs:1,0) -- (interpa);
   \draw[ultra thin, dashed] (axis cs:0,3) -- (interpb);
   \draw[ultra thin, dashed] (axis cs:3,0) -- (interpb);
\end{axis}
\end{tikzpicture}
\caption{}
\end{figure}
\end{document}
share|improve this question
    
Why do you decorate instead of putting arrows on the axis lines? Besides, if there are two values automation is not worth it? Do you have sometinhg else in mind? –  percusse Jul 26 at 18:20
    
@percusse It's a solution I took directly from another question which I didn't want to break with whatever solution one could come up with to my question here. I'm sure there are alternative ways to customize the axis arrows, but this is the one I found. The automation is for extended (more complicated) versions. I thought I'd start off with something simple. :-) –  1010011010 Jul 26 at 19:23

1 Answer 1

up vote 4 down vote accepted

Are you looking for the xticklabels and yticklabels option? The ticklabels in the xticklabels list are assigned to the tick positions given by the xtick list.

Example:

xtick={10,30},
xticklabels={$V_i=10$,$V_f=30$},

enter image description here

Code

\documentclass[margin=10pt]{standalone}
\usepackage{pgfplots} % loads also tikz
\pgfplotsset{compat=1.10}
\usetikzlibrary{arrows,arrows.meta}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    axis lines=left,
    axis line style=-{Latex[scale=1.5]},
    width=.95\linewidth,
    xmin = 0, xmax = 40,
    ymin = 0, ymax = 40,
    xtick={10,30},
    xticklabels={$V_i=10$,$V_f=30$},
    ytick={10,30},
    yticklabels={$P_i=10$,$P_f=30$},
    xlabel={Volume},
    ylabel={Pressure}
  ]
  \addplot[very thick,-latex ] coordinates{(10,10) (30,30)}
    coordinate[at start](interpa)coordinate[at end](interpb);
  \pgfplotsinvokeforeach {a,b}{
    \draw[ultra thin, dashed] 
      ({axis cs:0,0}|-interp#1)--(interp#1)--(interp#1|-{axis cs:0,0});}
\end{axis}
\end{tikzpicture}
\end{document}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.