0

I want colours to be assigned to all the different bars (ranging from B_{1} to B_{15}) in my number line.

% !TeX program = lualatex
\documentclass[a4paper,14pt]{extarticle}
\usepackage{mathptmx}% http://ctan.org/pkg/mathptmx% is TIME NEW ROMANS
\usepackage{tgtermes} % times font
%\usepackage{wordlike}% Emulate MS Word
\usepackage{setspace}
% Double spacing
%\doublespacing
%% One and half spacing
\onehalfspacing

% the below reduce space between section header and text \usepackage{titlesec} \titlespacing{\section}{0pt}{\parskip}{\parskip} \titlespacing{\subsection}{0pt}{\parskip}{\parskip} \titlespacing{\subsubsection}{0pt}{\parskip}{\parskip}

\usepackage[left=2cm,right=2cm,top=2cm,bottom=2cm]{geometry} \usepackage{amssymb,amsfonts,amsmath,graphicx,autobreak} \usepackage{changepage,mathtools,amsthm,tikz,blindtext} \usepackage{inputenc,float,caption,subcaption,enumitem} \usepackage[font=large,labelfont=bf,font=normalfont,labelsep=colon]{caption} \usepackage[font=small,labelfont=bf,font=bf,labelsep=colon]{subcaption} \usepackage{csquotes} %for single quoatation mark \usepackage{tikz} \begin{document} \begin{figure}[H] \begin{center} \begin{tikzpicture} % draw horizontal line

        \draw (0,0) -- (17,0);



        % draw vertical lines
        \foreach \x in {0.5,1.5,2.5,3.5,4.5,5.5,6.5,7.5,8.5,9.5,10.5,11.5,12.5,13.5,14.5,15.5,16.5,17.5}
        \draw (\x cm,5pt) -- (\x cm,-0pt);
        % draw nodes
        \draw (0.5,0) node[above=5pt] {$ x_{1} $} node[above=5pt] {$\rm   $};
        \draw (1.5,0) node[above=5pt] {$ x_{2} $} node[above=5pt] {$\rm   $};
        \draw (2.5,0) node[above=5pt] {$ x_{3} $} node[above=5pt] {$\rm   $};
        \draw (3.5,0) node[above=5pt] {$ x_{4} $} node[above=5pt] {$\rm   $};
        \draw (4.5,0) node[above=3pt] {$ x_{5} $} node[above=5pt] {$\rm   $};
        \draw (5.5,0) node[above=5pt] {$ x_{6} $} node[above=5pt] {$\rm   $};
        \draw (6.5,0) node[above=3pt] {$ x_{7} $} node[above=5pt] {$\rm   $};
        \draw (7.5,0) node[above=5pt] {$ x_{8} $} node[above=5pt] {$\rm   $};
        \draw (8.5,0) node[above=3pt] {$ x_{9} $} node[above=5pt] {$\rm   $};
        \draw (9.5,0) node[above=3pt] {$ x_{10} $} node[above=5pt] {$\rm   $};
        \draw (10.5,0) node[above=3pt] {$ x_{l1} $} node[above=5pt] {$\rm   $};
        \draw (11.5,0) node[above=3pt] {$ x_{l2} $} node[above=5pt] {$\rm   $};
        \draw (12.5,0) node[above=3pt] {$ x_{l3} $} node[above=5pt] {$\rm   $};
        \draw (13.5,0) node[above=3pt] {$ x_{14} $} node[above=5pt] {$\rm   $};
        \draw (14.5,0) node[above=3pt] {$ x_{l5} $} node[above=5pt] {$\rm   $};
        \draw (15.5,0) node[above=3pt] {$ x_{16} $};
        \draw (16.5,0) node[above=5pt] {$ x_{n} $} node[above=5pt] {$\rm   $};

        \draw [-] (0.5,-0.1) -- (0.5,-0.5);
        \draw [-] (2.5,-0.1) -- (2.5,-0.5);
        \draw [-] (0.5,-0.3) --node [midway,anchor=center,fill=white,inner sep=0.5pt] {$ B_{1} $} (2.5,-0.3); %1

        \draw [-] (1.5,0.9) -- (1.5,0.5);
        \draw [-] (3.5,0.9) -- (3.5,0.5);
        \draw [-] (1.5,0.7) --node [midway,anchor=center,fill=white,inner sep=0.5pt] {$ B_{2} $} (3.5,0.7); %2

        \draw [-] (2.5,-0.6) -- (2.5,-1.0);
        \draw [-] (4.5,-0.6) -- (4.5,-1.0);
        \draw [-] (2.5,-0.8) --node [midway,anchor=center,fill=white,inner sep=0.5pt] {$ B_{3} $} (4.5,-0.8); %3

        \draw [-] (3.5,1.0) -- (3.5,1.4);
        \draw [-] (5.5,1.0) -- (5.5,1.4);
        \draw [-] (3.5,1.2) --node [midway,anchor=center,fill=white,inner sep=0.5pt] {$ B_{4} $} (5.5,1.2); %4

        \draw [-] (4.5,-0.1) -- (4.5,-0.5);
        \draw [-] (6.5,-0.1) -- (6.5,-0.5);
        \draw [-] (4.5,-0.3) --node [midway,anchor=center,fill=white,inner sep=0.5pt] {$ B_{5} $} (6.5,-0.3); %5

        \draw [-] (5.5,0.9) -- (5.5,0.5);
        \draw [-] (7.5,0.9) -- (7.5,0.5);
        \draw [-] (5.5,0.7) --node [midway,anchor=center,fill=white,inner sep=0.5pt] {$ B_{6} $} (7.5,0.7); %6

        \draw [-] (6.5,-0.6) -- (6.5,-1.0);
        \draw [-] (8.5,-0.6) -- (8.5,-1.0);
        \draw [-] (6.5,-0.8) --node [midway,anchor=center,fill=white,inner sep=0.5pt] {$ B_{7} $} (8.5,-0.8); %7

        \draw [-] (7.5,1.0) -- (7.5,1.4);
        \draw [-] (9.5,1.0) -- (9.5,1.4);
        \draw [-] (7.5,1.2) --node [midway,anchor=center,fill=white,inner sep=0.5pt] {$ B_{8} $} (9.5,1.2); %8

        \draw [-] (8.5,-0.1) -- (8.5,-0.5);
        \draw [-] (10.5,-0.1) -- (10.5,-0.5);
        \draw [-] (8.5,-0.3) --node [midway,anchor=center,fill=white,inner sep=0.5pt] {$ B_{9} $} (10.5,-0.3); %9

        \draw [-] (9.5,0.9) -- (9.5,0.5);
        \draw [-] (11.5,0.9) -- (11.5,0.5);
        \draw [-] (9.5,0.7) --node [midway,anchor=center,fill=white,inner sep=0.5pt] {$ B_{10} $} (11.5,0.7); %10

        \draw [-] (10.5,-0.6) -- (10.5,-1.0);
        \draw [-] (12.5,-0.6) -- (12.5,-1.0);
        \draw [-] (10.5,-0.8) --node [midway,anchor=center,fill=white,inner sep=0.5pt] {$ B_{11} $} (12.5,-0.8); %11

        \draw [-] (11.5,1.0) -- (11.5,1.4);
        \draw [-] (13.5,1.0) -- (13.5,1.4);
        \draw [-] (11.5,1.2) --node [midway,anchor=center,fill=white,inner sep=0.5pt] {$ B_{12} $} (13.5,1.2); %12

        \draw [-] (12.5,-0.1) -- (12.5,-0.5);
        \draw [-] (14.5,-0.1) -- (14.5,-0.5);
        \draw [-] (12.5,-0.3) --node [midway,anchor=center,fill=white,inner sep=0.5pt] {$ B_{13} $} (14.5,-0.3); %13

        \draw [-] (13.5,0.9) -- (13.5,0.5);
        \draw [-] (15.5,0.9) -- (15.5,0.5);
        \draw [-] (13.5,0.7) --node [midway,anchor=center,fill=white,inner sep=0.5pt] {$ B_{14} $} (15.5,0.7); %14

        \draw [-] (14.5,-0.6) -- (14.5,-1.0);
        \draw [-] (16.5,-0.6) -- (16.5,-1.0);
        \draw [-] (14.5,-0.8) --node [midway,anchor=center,fill=white,inner sep=0.5pt] {$ B_{15} $} (16.5,-0.8); %15

    \end{tikzpicture}
    \caption{ Moving Block Bootstrap: Block size $= 2$, Overlapping $= 1$}
    \label{fig1}
\end{center}
\end{figure}

\end{document}

Number line

I want the colour of the bar B_{1} to be different from bar B_{1} and so forth.

I also want the font colour for the number line itself ($x_{1}, x_{2}, \dots, x_{n}$) to be uniform while the font colour for the bars ($B_{1}, B_{2}, \dots, B_{15}$) to be uniformly different also.

  • 2
    A couple of unrelated comments: 1) the mathptmx package is considered obsolete, I think \usepackage{newtxmath} is recommended instead (see https://ctan.org/pkg/mathptmx). 2) All the node[above=5pt] {$\rm $} are unnecessary, and note that \rm has been considered deprecated since the 90s. If it's math, probably use \mathrm{...} instead of \rm .... 3) Finally, inside figure environments \centering is recommended over a center environment: https://tex.stackexchange.com/a/2652 – Torbjørn T. Jun 28 '21 at 15:52

1 Answers1

3

You mean something like this (colors are random = proof of concept):

\documentclass[border=3.14mm,tikz]{standalone}
\definecolor{color1}{RGB}{125,125,234}
\definecolor{color3}{RGB}{0,34,212}
\definecolor{color5}{RGB}{25,125,234}
\definecolor{color7}{RGB}{18,128,200}
\definecolor{color9}{RGB}{125,25,234}
\definecolor{color11}{RGB}{125,25,234}
\definecolor{color13}{RGB}{125,125,34}
\definecolor{color15}{RGB}{5,125,34}
\definecolor{color2}{RGB}{125,125,234}
\definecolor{color4}{RGB}{0,34,212}
\definecolor{color6}{RGB}{25,125,234}
\definecolor{color8}{RGB}{18,128,200}
\definecolor{color10}{RGB}{125,25,234}
\definecolor{color12}{RGB}{125,25,234}
\definecolor{color14}{RGB}{125,125,34}

\begin{document} \begin{tikzpicture}[blabel/.style={midway,anchor=center,fill=white,inner sep=0.5pt}] \draw (0,0) -- (17,0); \foreach \x [count=\i] in {0.5,1.5,...,15.5} { \draw(\x,0) node[above=5pt] {$x_{\i}$} -- ++(0,5pt); } \draw(16.5,0) node[above=5pt] {$x_{n}$} -- ++(0,5pt);

    \foreach \x [count=\i] in {1,3,...,15}
    {
        \draw[|-|,color\x] (\x-0.5,{-0.55-0.25*(-1)^\i}) -- node[blabel,text=color\x] {$B_{\x}$} ++(2,0);
    }
    \foreach \x [count=\i] in {2,4,...,14}
    {
        \draw[|-|,color\x] (\x-0.5,{1.05-0.25*(-1)^(\i+1)}) -- node[blabel,text=color\x] {$B_{\x}$} ++(2,0);
    }   
\end{tikzpicture}

\end{document}

EDIT: You can also combine the loops for the labels using a combination of slightly more "advanced" mathematical functions:

\foreach \x in {1,2,...,15}
        {
            \draw[|-|,color\x] (\x-0.5,{0.55*((-1)^\x)-0.5*(Mod(\x,2)-1)-0.25*(-1)^(int(\x/2)+1)}) -- node[blabel,text=color\x] {$B_{\x}$} ++(2,0);
        }

EDIT2: To have a bit more flexibility in terms of barlength, you can use \pgfmatsetmacros:

\begin{tikzpicture}[blabel/.style={midway, anchor=center, fill=white, inner sep=0.5pt}]
        \newcommand*{\barlength}{3}%
        \pgfmathsetmacro{\barnumber}{16/\barlength}%
        \draw (0,0) -- (17,0);
        \foreach \x [count=\i] in {0.5,1.5,...,15.5}
        {\draw(\x,0) node[above=5pt] {$x_{\i}$} -- ++(0,5pt);}
        \draw(16.5,0) node[above=5pt] {$x_{n}$} -- ++(0,5pt);
    \foreach \x in {1,2,...,\barnumber}
    {
        \draw[|-|,color\x] ({\x-0.5+(\x-1)*(\barlength-1)},{0.55*((-1)^\x)-0.5*(Mod(\x,2)-1)-0.25*(-1)^(int(\x/2)+1)}) -- node[blabel,text=color\x] {$B_{\x}$} ++(\barlength,0);
    }
\end{tikzpicture}

Markus G.
  • 2,735
  • I love your automation strategy. – Daniel James Jun 28 '21 at 19:11
  • I am able to change the span of the bars for example say I want to set it to 4 instead of 2 like this: \foreach \x in {1,2,...,15} { \draw[|-|,color\x] (\x-0.5,{0.55*((-1)^\x)-0.5*(Mod(\x,2)-1)-0.25*(-1)^(int(\x/2)+1)}) -- node[blabel,text=color\x] {$B_{\x}$} ++(4,0); } How do I chang the length of overlapping to 2 in my own case? – Daniel James Jun 28 '21 at 19:16
  • I could have asked for it in a fresh question, but your answer's approach excites me. – Daniel James Jun 28 '21 at 19:36
  • Yes, it is borderline a new question. If I get your question correctly, you want to have longer bars, so essentially you'd have to adjust the x-value to account for the additional required shift:\draw[|-|,color\x] ({\x-0.5+(\x-1)*3},{0.55*((-1)^\x)-0.5*(Mod(\x,2)-1)-0.25*(-1)^(int(\x/2)+1)}) -- node[blabel,text=color\x] {$B_{\x}$} ++(4,0); – Markus G. Jun 28 '21 at 19:40
  • In the same vein with my question in the comment, let say I do not want bars to overlap, with your set bar length of 2, how do I just them? – Daniel James Jun 28 '21 at 19:41
  • Again, there is only the need to adjust the x-value of the \draw command. If you want the length to be 2 and the bars not overlap: {\x-0.5+(\x-1)} – Markus G. Jun 28 '21 at 19:44
  • Thank you for your attention. – Daniel James Jun 28 '21 at 19:48