So, whenever I create a xbar plot with long labels it happens to me that the tikzpicture is scaled too wide because (I guess) textwidth
only applies to the axis but not the labels. So I thought I substract the width of the longest label from textwidth, but then the width is too small. My question is:
- how can I get the "real" textwidth?
- if possible, how can I get that automatically without entering the longest label by hand
\documentclass[12pt,a4paper]{scrreprt}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{filecontents}
\usepackage{lipsum}
\begin{filecontents*}{species.dat}
species,no_dbh
Fagus sylvatica,0.746478873239437
Pinus sylvestris,0.633802816901408
Quercus petraea,0.563380281690141
Pseudotsuga menziesii,0.126760563380282
\end{filecontents*}
\begin{document}
\lipsum[1-1]
\begin{figure}[hpbt]
\pgfplotstableread[col sep=comma]{species.dat}{\steigi}
\centering
\begin{tikzpicture}
\begin{axis}[
xbar,
x tick label style={font=\footnotesize},
y tick label style={font=\scriptsize},
height=5cm,
width=\textwidth,
xmajorgrids=true,
major grid style={dotted, very thick},
axis x line*=bottom,
axis y line*=none,
bar width=5pt,
symbolic y coords={Fagus sylvatica,Pinus sylvestris,Quercus petraea,Pseudotsuga menziesii},
ytick=data,
xticklabel={\pgfmathparse{\tick*100} pgfmathprintnumber{\pgfmathresult}\%},
y dir=reverse,
enlarge y limits=0.2,
xmax=0.85,
xmin=0,
nodes near coords,
every node near coord/.append style={anchor=west,font=\scriptsize},
nodes near coords align={horizontal},
point meta=x*100,
nodes near coords=\pgfmathprintnumber{\pgfplotspointmeta}\%
]
\addplot[fill=gray] table [x=no_dbh,y=species]{\steigi};
\end{axis}
\end{tikzpicture}
\caption{Tree species probability}
\end{figure}
\lipsum[2-2]
\end{document}
My solution is too use width=\textwidth-width("Pseudotsuga menziesii")
but then it's scalled too small.
width=\textwidth-width("Pseudotsuga menziesii")
.