Search code examples
latexcentering

How can I place a figure on top of the page?


I am currently working on this code (this is a shortened version):

\documentclass{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
% Syntax:
% \DoublLine[half of the double line distance]{first node}{second node}{options line 1}{options line 2}
\newcommand\DoubleLine[5][4pt]{%
    \path(#2)--(#3)coordinate[at start](h1)coordinate[at end](h2);
    \draw[#4]($(h1)!#1!90:(h2)$)--($(h2)!#1!-90:(h1)$); 
    % node [midway, above=1pt, fill=none] {3};
    \draw[#5]($(h1)!#1!-90:(h2)$)--($(h2)!#1!90:(h1)$); 
    % node [midway, below=1pt, fill=none] {3};
    }
\begin{figure}[h]
    \begin{tikzpicture}[myn/.style={very thick,draw,inner sep=0.25cm,outer sep=3pt}]
    \scalebox{0.5}{
\centering
     % place nodes
\node[myn]  (a) at (2,5) {Node 6};
    \node[myn]  (b) at  (4, 8)   {Node 12};
    \node[myn]  (c) at  (5, 5)  {Node 19};
    \node[myn]  (d) at  (5, 3)  {Node 20};
    \node[myn]  (e) at  (7.5,5)  {Node 18};
    \node[myn]  (f) at  (7.5,10)  {Node 10};
\node[myn]  (g) at  (10,3)  {Node 4};
    \node[myn]  (h) at  (10,5)  {Node 2};
    \node[myn]  (i) at  (10,8)  {Node 13};
    \node[myn]  (j) at  (10,10)  {Node 14};
    \node[myn]  (k) at  (13,5)  {Node 21};
    \node[myn]  (l) at  (12,7)  {Node 1};
\node[myn]  (m) at  (12,10)  {Node 16};
    \node[myn]  (n) at  (15.5,5)  {Node 3};
    \node[myn]  (o) at  (13,12)  {Node 8};
    \node[myn]  (p) at  (16,3)  {Node 5};
    \node[myn]  (q) at  (16,7)  {Node 9};
    \node[myn]  (r) at  (7.5,3)  {Node 11};
\node[myn]  (s) at  (11,1){Node 7};
    \node[myn]  (t) at  (12,-1){Node 17};
    \node[myn]  (u) at  (13,-3){Node 22};
    \node[myn]  (v) at  (7.5,-1){Node 15};     

%Single line orange
\draw[edge][color=orange, very thick] (i)--(j);

%Single line orange 
\draw[edge][color=orange, very thick] (n)--(q);
\draw[edge][color=orange, very thick] (h)--(d);

%double line orange
    \DoubleLine{o}{m}{-,very thick,cyan}{-,very thick,orange};
    \DoubleLine{i}{m}{-,very thick,cyan}{-,very thick,orange};
    

    
}
    \end{tikzpicture}
    \caption{Line plan: Model A on Set RL}
    \label{fig:Line plan Model A on Set RL}
\end{figure}
\end{document}

The figure is shown in the middle of the page and not even centered. As I am quite a beginner in latex, I am wondering how this could work? I need the figure to be on top of the page and centered.

I tried [h] and [t] and also \vspace*{3in}, but it does not work.


Solution

  • A couple of problems:

    • your figure is too large, it includes tons of white space caused by some of the problem mentioned in the points below. You can see a warning about the overfull box in the log file. Thus it can't be placed according to your [t] floating specifier

    • using \centering and \scalebox inside the tikz picture makes no sense. If you want the picture to be centred, use it before the tikzpicture

    • don't use \scalebox for elements which contain text.

    • tikz has it's own commands to scale things

    • if you want a figure to be at the top of the page, the page actually needs some text on it

    and finally:

    • The code does NOT run if it throws an error message! Latex only syntax checks the rest of the document, not necessarily producing sensible output. Never ignore error messages.

    \documentclass{scrartcl}
    \usepackage[utf8]{inputenc}
    \usepackage{tikz}
    \usetikzlibrary{calc}
    \begin{document}
    % Syntax:
    % \DoublLine[half of the double line distance]{first node}{second node}{options line 1}{options line 2}
    \newcommand\DoubleLine[5][4pt]{%
        \path(#2)--(#3)coordinate[at start](h1)coordinate[at end](h2);
        \draw[#4]($(h1)!#1!90:(h2)$)--($(h2)!#1!-90:(h1)$); 
        % node [midway, above=1pt, fill=none] {3};
        \draw[#5]($(h1)!#1!-90:(h2)$)--($(h2)!#1!90:(h1)$); 
        % node [midway, below=1pt, fill=none] {3};
        }
        
    some text
        
    \begin{figure}[t]
    \centering
    \begin{tikzpicture}[scale=0.5,transform shape,myn/.style={very thick,draw,inner sep=0.25cm,outer sep=3pt}]
    \node[myn]  (a) at (2,5) {Node 6};
        \node[myn]  (b) at  (4, 8)   {Node 12};
        \node[myn]  (c) at  (5, 5)  {Node 19};
        \node[myn]  (d) at  (5, 3)  {Node 20};
        \node[myn]  (e) at  (7.5,5)  {Node 18};
        \node[myn]  (f) at  (7.5,10)  {Node 10};
    \node[myn]  (g) at  (10,3)  {Node 4};
        \node[myn]  (h) at  (10,5)  {Node 2};
        \node[myn]  (i) at  (10,8)  {Node 13};
        \node[myn]  (j) at  (10,10)  {Node 14};
        \node[myn]  (k) at  (13,5)  {Node 21};
        \node[myn]  (l) at  (12,7)  {Node 1};
    \node[myn]  (m) at  (12,10)  {Node 16};
        \node[myn]  (n) at  (15.5,5)  {Node 3};
        \node[myn]  (o) at  (13,12)  {Node 8};
        \node[myn]  (p) at  (16,3)  {Node 5};
        \node[myn]  (q) at  (16,7)  {Node 9};
        \node[myn]  (r) at  (7.5,3)  {Node 11};
    \node[myn]  (s) at  (11,1){Node 7};
        \node[myn]  (t) at  (12,-1){Node 17};
        \node[myn]  (u) at  (13,-3){Node 22};
        \node[myn]  (v) at  (7.5,-1){Node 15};     
    
    %Single line orange
    \draw[][color=orange, very thick] (i)--(j);
    
    %Single line orange 
    \draw[][color=orange, very thick] (n)--(q);
    \draw[][color=orange, very thick] (h)--(d);
    
    %double line orange
        \DoubleLine{o}{m}{-,very thick,cyan}{-,very thick,orange};
        \DoubleLine{i}{m}{-,very thick,cyan}{-,very thick,orange};
        \end{tikzpicture}
        \caption{Line plan: Model A on Set RL}
        \label{fig:Line plan Model A on Set RL}
    \end{figure}
    \end{document}
    

    enter image description here