1

I have these two standalone bar graphs that are identical in size, but when I \includestandalone{} them, to insert them into my main document, I can't for the life of me get them to line up.

If I put them both in the same standalone document and import them that way, they're perfectly in line. But they're two separate figures and i want to be able to control them as separate entities. Is there something I can do to neaten it up?

I know it's only a very small thing, but a) it looks careless. And b) the whole idea is to stack them perfectly so the reader can compare and contrast the two sets.


Here they are included together as a single standalone document, perfectly aligned:

bargraphs.tex enter image description here


Here they are included as two separate standalone documents, not co-operating:

bargraph1.tex + bargraph2.tex

enter image description here


Here's the code for the bar graphs together in a standalone:

bargraphs.tex

\documentclass[margin=10,tikz]{standalone}
\usepackage{tikz}
\tikzset{barlabels/.style={font=\footnotesize\sffamily}, 
declare function={barheight=5pt;}}

\begin{document}
\begin{tikzpicture}[y=0.3cm, x=0.06cm]
\begin{scope}[yshift=-0cm]
\foreach [count=\i from 0] \p/\t in{
    500/Marriott (2018),
    3000/Yahoo! (2017),
    143/Equifax (2017),
    412/FriendFinder (2016),
    145/eBay (2014),
    110/Target (2013)}
{\node [anchor=base east, barlabels, name=i-\i] at (0,-\i) {\t};
\fill [blue!40] (i-\i.base east) rectangle ++({(1*(\p)/25)*0.6},barheight) ++(0,-barheight) node[barlabels, black, anchor=base west] {\p\ M};
}
\end{scope}
\begin{scope}[yshift=-2cm]
\foreach [count=\i from 0] \p/\t in{
    110/Target (2013),
    38/Adobe (2013),
    77/Sony (2011),
    40/RSA Security (2011),
    134/Heartland (2008),
    94/TJX (2006)}
{\node [anchor=base east, barlabels, name=i-\i] at (0,-\i) {\t};
\fill [red!50] (i-\i.base east) rectangle ++({((\p)-13)*0.6},barheight) ++(0,-barheight) node[barlabels, black, anchor=base west] {\p\ M};
}
\end{scope}
\end{tikzpicture}
\end{document}

Here they are separately:

bargraph1.tex

\documentclass[margin=10,tikz]{standalone}
\usepackage{tikz}
\tikzset{barlabels/.style={font=\footnotesize\sffamily}, 
declare function={barheight=5pt;}}

\begin{document}
\begin{tikzpicture}[y=0.3cm, x=0.05cm]
\begin{scope}[yshift=-4cm]
\foreach [count=\i from 0] \p/\t in{
    500/Marriott (2018),
    3000/Yahoo! (2017),
    143/Equifax (2017),
    412/FriendFinder (2016),
    145/eBay (2014),
    110/Target (2013)}
{\node [anchor=base east, barlabels, name=i-\i] at (0,-\i) {\t};
\fill [blue!40] (i-\i.base east) rectangle ++({(1*(\p)/25)*0.6},barheight) ++(0,-barheight) node[barlabels, black, anchor=base west] {\p\ M};
}
\end{scope}
\end{tikzpicture}
\end{document}

bargraph2.tex

\documentclass[margin=10,tikz]{standalone}
\usepackage{tikz}
\tikzset{barlabels/.style={font=\footnotesize\sffamily}, 
declare function={barheight=5pt;}}

\begin{document}
\begin{tikzpicture}[y=0.3cm, x=0.05cm]
\begin{scope}[yshift=-4cm]
\foreach [count=\i from 0] \p/\t in{
    110/Target (2013),
    38/Adobe (2013),
    77/Sony (2011),
    40/RSA Security (2011),
    134/Heartland (2008),
    94/TJX (2006)}
{\node [anchor=base east, barlabels, name=i-\i] at (0,-\i) {\t};
\fill [red!50] (i-\i.base east) rectangle ++({((\p)-13)*0.6},barheight) ++(0,-barheight) node[barlabels, black, anchor=base west] {\p\ M};
}
\end{scope}
\end{tikzpicture}
\end{document}

main.tex

Here's an MWE of the main.tex document:

\documentclass[apacite,jou,a4paper]{apa6}
\usepackage{amsmath,amssymb,booktabs}  
\usepackage[table,xcdraw]{xcolor}
\usepackage[utf8]{inputenc} %useful to type directly diacritic characters
\usepackage{tikz}
\tikzset{barlabels/.style={font=\footnotesize\sffamily}, 
declare function={barheight=5pt;}}
\usepackage{standalone}

\title{Title}

\begin{document}
\maketitle    

\section{Heading}

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\\

\noindent
\centering
\includestandalone{test/bargraph1}
\\
\noindent
\centering
\includestandalone{test/bargraph2}
\\

\section{Heading}

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. \\

\bibliography{references}

\end{document}
  • Why did you use different rectangle dimension calculations? In bargraph1.tex ({(1*(\p)/25)*0.6},barheight) and in bargraph2.tex `({((\p)-13)*0.6},barheight). – ferahfeza Apr 10 at 0:54
  • @ferahfeza to make them the same size. Look at the plotted values. Look at 110 M. See how the scales are different? – tjt263 Apr 10 at 1:04
  • 1
    I see. The maximum value in first graph is 3000, in second graph is 134. For same size, I used the ratio for second graph is ({\p/1.1166666666666667)*0.6},barheight). But the text used in graph 3000 M and 134 M. These text lengths disrupt the ratio again. Therefore I used a coefficient of 0.63 ~ 0.65 instead of a coefficient of 0.6, i.e, the formula I used is, for example ({\p/1.1166666666666667)*0.63},barheight). – ferahfeza Apr 10 at 1:08
  • It was supposed to be 3 B (for Billion) but it seems I can't use the loop and then go back and customize whatever I want. – tjt263 Apr 10 at 1:24
  • @ferahfeza Holy crap, you were right though. How'd you know about the coefficient or whatever? And why was it lining up properly for me when they were both in the same standalone document, but not separately? It's frustrating. I really need to find a more more predictable and reliable way to create documents. I just spent hours trying to move this bloody thing a 25th of an inch. – tjt263 Apr 10 at 1:57
1

This creates two tikzpictures with common left and right borders. I first printed the borders for each tikzpicture and took the larger (more extreme) of the two. The right border can affect scaling or centering.

\documentclass[tikz]{standalone}
\tikzset{barlabels/.style={font=\footnotesize\sffamily}, 
declare function={barheight=5pt;}}

\newcommand{\printborder}{% must be run inside a tikzpicture
  \path (current bounding box.south west);
  \pgfgetlastxy{\west}{\south}
  \path (current bounding box.north east);
  \pgfgetlastxy{\east}{\north}
  \node[above] at (current bounding box.north) {(\west, \south) (\east, \north)};
}

\begin{document}
\begin{tikzpicture}[y=0.3cm, x=0.06cm]
\foreach [count=\i from 0] \p/\t in{
    500/Marriott (2018),
    3000/Yahoo! (2017),
    143/Equifax (2017),
    412/FriendFinder (2016),
    145/eBay (2014),
    110/Target (2013)}
{\node [anchor=base east, barlabels, name=i-\i] at (0,-\i) {\t};
\fill [blue!40] (i-\i.base east) rectangle ++({(1*(\p)/25)*0.6},barheight) ++(0,-barheight) node[barlabels, black, anchor=base west] {\p\ M};
}
%\printborder
\path (-81pt,0) (158pt,0);% set common west and east borders
\end{tikzpicture}

\begin{tikzpicture}[y=0.3cm, x=0.06cm]
\foreach [count=\i from 0] \p/\t in{
    110/Target (2013),
    38/Adobe (2013),
    77/Sony (2011),
    40/RSA Security (2011),
    134/Heartland (2008),
    94/TJX (2006)}
{\node [anchor=base east, barlabels, name=i-\i] at (0,-\i) {\t};
\fill [red!50] (i-\i.base east) rectangle ++({((\p)-13)*0.6},barheight) ++(0,-barheight) node[barlabels, black, anchor=base west] {\p\ M};
}
%\printborder
\path (-81pt,0) (158pt,0);% set common west and east borders
\end{tikzpicture}
\end{document}
  • Thanks. How did you print the border distances in the first place? Is that what's commented out? I haven't tried it yet but it sounds promising. Although, this looks like they're both in the same standalone document. Which for me ligned up perfectly anyway (see the first image above). The problem was importing them as two separate objects (see the second image above). – tjt263 Apr 10 at 7:35
  • Each page of a standalone document is a different image with different sizes. You can use \includegraphics[page=2]{...} for example to get just one. – John Kormylo Apr 10 at 15:01

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.