2

I'm looking for a solution to restrict tick labels to be within the axis limits (see figure, without enlarging limits). enter image description here

The idea was to apply a custom tick label style for example to the first tick:

\documentclass{report}
\usepackage{pgf}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
\begin{axis}[%
scale only axis,
xmin=-1000,
xmax=1000,
ymin=0,
ymax=5,
xtick={-1000,-500,...,1000},
x tick label style={xshift={\pgfmathparse{\ticknum == 0 ? "10pt" : "0pt"}\pgfmathresult}},
]    
\draw (-300,0.5)--(1000,1);
\end{axis}
\end{tikzpicture}
\end{document}  

Unfortnunately this gives an "! Undefined control sequence." Applying a similar code to the label text itself works well. The use of extra ticks is not favored ( pgfplots: How to specify custom tick and label at specified value on an axis ) since the fist and last tick must have different styles. So the question is, how to customize tick label styles for individual tick labels depending on their value or position?

Thanks for any help

3

I think it is easier to use extra x ticks

extra x ticks={-1000,1000},
extra x tick labels={\rlap{-1000},\llap{1000}}

enter image description here

Code:

\documentclass{report}
\usepackage{pgf}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
\begin{axis}[%
scale only axis,
xmin=-1000,
xmax=1000,
ymin=0,
ymax=5,
xtick={-500,0,500},
extra x ticks={-1000,1000},
extra x tick labels={\rlap{-1000},\llap{1000}}
]    
\draw (-300,0.5)--(1000,1);
\end{axis}
\end{tikzpicture}
\end{document}

Another possibility is

xtick={-1000,-500,...,1000},
xticklabels={\rlap{-1000},500,0,500,\llap{1000}}

But if the xshift of the labels should depend on \ticknum you could use something like

x tick label style={xshift={\ticknum == 0 ? "15pt" : (\ticknum == 4 ? "-10pt" : "0pt")}}

enter image description here

Code:

\documentclass{report}
\usepackage{pgf}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
\begin{axis}[%
scale only axis,
xmin=-1000,
xmax=1000,
ymin=0,
ymax=5,
xtick={-1000,-500,...,1000},
x tick label style={xshift={\ticknum == 0 ? "15pt" : (\ticknum == 4 ? "-10pt" : "0pt")}},
]    
\draw (-300,0.5)--(1000,1);
\end{axis}
\end{tikzpicture}
\end{document}

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.