7

I would like to create custom function to create objects. Let me explain better, consider the following code:

\documentclass[tikz,border={12pt,12pt}]{standalone}

\usepackage{pgfplots}
\usetikzlibrary{calc}

\begin{document}

\begin{tikzpicture}

    % Min/Max
    \pgfmathsetmacro{\Min}{1.5}
    \pgfmathsetmacro{\Max}{2.0}
    % Tick distance
    \pgfmathsetmacro{\XTickDistance}{
        (\Min - \Max) / (10 - 1)
    }

    \begin{axis}[
    hide axis, scale only axis, height=0pt, width=0pt, % hide axis
    colormap/jet,
    colorbar sampled,
    colorbar horizontal,
    point meta min=\Min,
    point meta max=\Max,
    colorbar style = {
        samples = 9,
        height = 0.5cm,
        width = 8cm,
        xtick style = {draw=none},
        xticklabel style = {
            text width = 2.5em,
            align = center,
            /pgf/number format/.cd,
                fixed,
                fixed zerofill,
                precision = 2,
            /tikz/.cd
        },
        xtick distance=\XTickDistance,
    }
    ]
    \addplot [draw=none] coordinates {(0,0)};
    \end{axis}

\end{tikzpicture}

\end{document}

this will create a simple colorbar between 1.5 and 2.0 using the colormap jet.
I would like to create a function (named customFunc) that when invoked return a colorbar between two value, so the code will be just:

\documentclass[tikz,border={12pt,12pt}]{standalone}

\usepackage{pgfplots}
\usetikzlibrary{calc}

\begin{document}

\begin{tikzpicture}
    \customFunc{1.5, 2.0}
\end{tikzpicture}

\end{document}

The real code of customFunc would be in a different file

Thanks

7

Basically just do

\newcommand\MakeMeAColorbar[2]{ <the code that generates the colorbar> }

and add #1 and #2 where you have the min and max values. The [2] means that the macro has two default arguments, and #1/#2 refer to those. Use it as

\MakeMeAColorbar{1.5}{2}

Of course you can put the whole \newcommand in a separate file, e.g. MyMacros.tex, and have \input{MyMacros} in the preamble.

Complete example:

enter image description here

\documentclass[tikz,border={12pt,12pt}]{standalone}

\usepackage{pgfplots}

\newcommand\MakeMeAColorbar[2]{%
    % Min/Max
    \pgfmathsetmacro{\Min}{#1}
    \pgfmathsetmacro{\Max}{#2}
    % Tick distance
    \pgfmathsetmacro{\XTickDistance}{
        (\Min - \Max) / (10 - 1)
    }

    \begin{axis}[
    hide axis, scale only axis, height=0pt, width=0pt, % hide axis
    colormap/jet,
    colorbar sampled,
    colorbar horizontal,
    point meta min=\Min,
    point meta max=\Max,
    colorbar style = {
        samples = 9,
        height = 0.5cm,
        width = 8cm,
        xtick style = {draw=none},
        xticklabel style = {
            text width = 2.5em,
            align = center,
            /pgf/number format/.cd,
                fixed,
                fixed zerofill,
                precision = 2,
            /tikz/.cd
        },
        xtick distance=\XTickDistance,
    }
    ]
    \addplot [draw=none] coordinates {(0,0)};
    \end{axis}
}

\begin{document}

\begin{tikzpicture}

\MakeMeAColorbar{1.5}{2}

\end{tikzpicture}

\end{document}

Position

To set a specific position, you need a third argument as well. For example if you want the coordinate to be the first argument, you add

at={(#1)}

to colorbar style. Optionally add an anchor as well, to determine which part of the colorbar should be placed at that coordinate.

Complete code below. Note that there is a small vertical offset, the colorbar is placed a bit lower than it should be, and I'm not sure why that happens.

This time use it as \MakeMeAColorbar{<coordinate>}{<min>}{<max>}. If you want the <coordinate> to be an optional argument, change the first line of the definition to

\newcommand\MakeMeAColorbar[3][0,0]{

and use it as \MakeMeAColorbar[<coordinate>]{<min>}{<max>} or \MakeMeAColorbar{<min>}{<max>} depending on whether you want to specify the coordinate or not.

\documentclass{article}

\usepackage{pgfplots}

\newcommand\MakeMeAColorbar[3]{%
    % Min/Max
    \pgfmathsetmacro{\Min}{#2}
    \pgfmathsetmacro{\Max}{#3}
    % Tick distance
    \pgfmathsetmacro{\XTickDistance}{
        (\Min - \Max) / (10 - 1)
    }

    \begin{axis}[
    hide axis, scale only axis, height=0pt, width=0pt, % hide axis
    colormap/jet,
    colorbar sampled,
    colorbar horizontal,
    point meta min=\Min,
    point meta max=\Max,
    colorbar style = {
        at={(#1)},anchor=center,
        samples = 9,
        height = 0.5cm,
        width = 8cm,
        xtick style = {draw=none},
        xticklabel style = {
            text width = 2.5em,
            align = center,
            /pgf/number format/.cd,
                fixed,
                fixed zerofill,
                precision = 2,
            /tikz/.cd
        },
        xtick distance=\XTickDistance,
    }
    ]
    \addplot [draw=none] coordinates {(0,0)};
    \end{axis}
}

\begin{document}

\begin{tikzpicture}[remember picture]

\MakeMeAColorbar{current page.center}{1.5}{2}
\fill (current page.center) circle[radius=5pt];

\end{tikzpicture}

\end{document}
  • @yellowhat Updated answer. – Torbjørn T. Oct 4 '16 at 8:53

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.