2

I have strings for the X axis labels on a pgf line plot. Sometimes these are long and overlap each other. This question is already addressed on this site but only for static cases. The solution proposed is always either to move the labels around, manually truncate them, change their orientation or something similar.

I however am using this for a report generator where the array of label strings is added dynamically at runtime, so I do not have any information about how many labels there are or how long they are. I also can't angle them because that would change the layout and size of various elements which need to remain constant.

I was hoping there might already be a package or class around that does this. Basically I need to calculate the following (assuming monospace font):

  • How much total space is available along the x axis (TS)
  • How many labels are there (NL)
  • How much space per label (SL)=(TS/NL)
  • How big is one rendered character in the current font. (CS)
  • How many characters can fit in a label (CL)=(SL/CS)
  • Create a list of labels where each label is a truncated version of the original label using only the first (CL) characters.

It is always worth checking if something exists before reinventing the wheel. Here is an example of what the plot currently looks like:

\documentclass[a4paper, landscape, lmargin=0, rmargin=0]{article}
\usepackage{pgfplots}
\usepgfplotslibrary{patchplots}
\usepackage{textcomp}
\usepackage[margin=0.1cm]{geometry}
\pagestyle{empty}
\begin{document}
\begin{tikzpicture}   
    \begin{axis}[
        ymax=2.8,
        ymin=-2.8,
        label style={font=\small},
        tick label style={font=\small},
        ymajorgrids=true,
        xtick distance=1,
        xticklabels={,,LR Fascia,LR Fascia - Lt Qtr,Lt Fascia,Lt Fascia - Lt Fdr,Lt Fdr,Lt Qtr,RR Fascia,RR Fascia - Rt Qtr,Rt Fascia,Rt Fascia - Rt Fdr,Rt Fdr,Rt Qtr},
        width=20cm,
        height=9cm,
        ytick style={/pgfplots/major tick length=3mm},
        max space between ticks=10,
    ]

    \addplot[
    mark=*,
    ] coordinates {
        (1,-0.17)
        (2,1.04)
        (3,-1.08)
        (4,-2.2)
        (5,1.12)
        (6,-1.12)
        (7,-0.11)
        (8,0.15)
        (9,-0.43)
        (10,0.85)
        (11,-1.28)
        (12,-0.26)
    };

    \end{axis}
\end{tikzpicture}


\end{document}
2
  • Welcome to TeX.SE! When you say "truncate",do you mean literally to cut them off? (How would one then be able to read these labels?) And just to be sure: rotating these labels (by 90 degrees, say) is not an option for you?
    – user121799
    Commented Sep 27, 2018 at 15:31
  • Yes, literally cut them off. Ideally a function that returns a substring that never overlaps its neighbours, maybe with .. at the end. Given arbitrary label lengths and a constrained printable page layout, rotating the labels 90 degrees just means they overlap other elements on the page, instead of each other. There is a maximum label length that can reasonably be displayed and I need to find a way to deal with excess. Commented Oct 2, 2018 at 8:33

1 Answer 1

1

Well due to lack of interest in this question I have had to work on this myself. So far I have found the package trimclip which gets me a lot of the way there. Replacing the xLabels assignment with:

xticklabels={,,
    \clipbox{0pt 0pt \width-1cm 0mm}{LR Fascia},
    \clipbox{0pt 0pt \width-1cm 0mm}{LR Fascia - Lt Qtr},
    \clipbox{0pt 0pt \width-1cm 0mm}{Lt Fascia},
    \clipbox{0pt 0pt \width-1cm 0mm}{Lt Fascia - Lt Fdr},
    \clipbox{0pt 0pt \width-1cm 0mm}{Lt Fdr},
    \clipbox{0pt 0pt \width-1cm 0mm}{Lt Qtr},
    \clipbox{0pt 0pt \width-1cm 0mm}{RR Fascia},
    \clipbox{0pt 0pt \width-1cm 0mm}{RR Fascia - Rt Qtr},
    \clipbox{0pt 0pt \width-1cm 0mm}{Rt Fascia},
    \clipbox{0pt 0pt \width-1cm 0mm}{Rt Fascia - Rt Fdr},
    \clipbox{0pt 0pt \width-1cm 0mm}{Rt Fdr},
    \clipbox{0pt 0pt \width-1cm 0mm}{Rt Qtr},
},

Truncates the labels to 1cm in length.

I am not going to mark this as an accepted answer until I have resolved a few issues however:

  • I would prefer to have the 1cm value calculated by the latex code rather than have to set it manually. I am not sure the plot actually knows how big it will be at the stage this is evaluated so it may not be possible.

  • trimclip appears to destroy my formatting, if I set the labels to have a certain color e.g. every tick/.append style={green}, then they are turned black when using a clipbox

  • clipbox appears to cut the top off upper case characters even when vertical clipping is set to 0

Still its a better answer than I have gotten so far and it might be useful to someone

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .