2

I am making a figure in LaTeX of which the code is given below. In the end result, the figure is not centered in the page, the page offset from the left is more than the page offset from the right. Kindly tell me what can be a possible solution to this thing?

\documentclass[a4paper]{article}

\usepackage{tikz}
\usetikzlibrary{arrows}
\usepackage{verbatim}

\begin{document}
\pagestyle{empty}

\begin{figure}    

\tikzstyle{int}=[draw,  minimum size=2em]
\tikzstyle{init} = [pin edge={to-,thick,black}]
\tikzset{  sum/.style      = {draw, circle, node distance = 2cm}}

\begin{tikzpicture}[node distance=3cm,auto,>=latex']
\centering
\node at (-1,0)[int] (Source) {Source};
\node at (3,0)[int] (Modulation) {Modulation - QAM};
\node at (6,0)[circle,draw] (Adder) {\Large$+$};
\node at (6,-2)[int] (AWGN) {AWGN Channel};
\node at (8.7,0)[int] (Demodulation) {Demodulation};
\node at (13,0)[int] (BER) {BER Meter};

\draw[->](Source) -- node {$\overline{u}$}(Modulation);
\draw[->](Modulation) -- node {$\overline{x}$}(Adder);
\draw[->](AWGN) -- node {$\overline{n}$}(Adder);
\draw[->](Adder) -- node {$\overline{y}$}(Demodulation);
\draw[->](Demodulation) -- node {$  \hat{\overline{u}}  $}(BER);

\draw (0.85,0) -- (0.85,1.5);
\draw (0.85,1.5) -- (13,1.5);
\draw [->] (13,1.5) -- node {}(BER);

\end{tikzpicture}
\end{figure}

\end{document}

Thanks

2
  • 6
    Use \begin{figure}\centering, that is, move the center command outside the tikz but inside the figure.
    – Sigur
    Oct 19, 2014 at 18:25
  • It's still the same, didn't change. The offset is still the same.
    – asm_nerd1
    Oct 19, 2014 at 20:23

1 Answer 1

6

When trying to center something on your page, what TeX essentially does is balancing out spacing on both sides of the object across the horizontal typeset width. When the element you're trying to center is larger than this width, it will align to the left and overflow the margin on the right. In the picture below, I've already added the showframe package which outlines the horizontal width I speak of (the large block in which your tikzpicture resides). It also shows that indeed, the right margin is overflown:

http://i.imgur.com/KItAUqv.png?1

The fact your tikzpicture is in fact larger than the parameter for horizontal typeset width, \hsize, or the identical \linewidth (which is probably the more intuitive parameter for you), is even more easily shown by adding the scale key to your tikzpicture, and setting it to .5 (which is an arbitrary value). This makes the tikzpicture smaller than the horizontal typeset width. The picture below might contribute to the visual understanding that your tikzpicture is now centered:

http://i.imgur.com/aA5wc4V.png?2

\documentclass[a4paper]{article}

\usepackage{tikz}
\usetikzlibrary{arrows}
\usepackage{verbatim}
\usepackage{showframe}% added to show that the figure is being centered

\begin{document}
\pagestyle{empty}

\begin{figure}    

\tikzstyle{int}=[draw,  minimum size=2em]
\tikzstyle{init} = [pin edge={to-,thick,black}]
\tikzset{  sum/.style      = {draw, circle, node distance = 2cm}}

\centering
\begin{tikzpicture}[node distance=3cm,auto,>=latex',scale=.5]
\node at (-1,0)[int] (Source) {Source};
\node at (3,0)[int] (Modulation) {Modulation - QAM};
\node at (6,0)[circle,draw] (Adder) {\Large$+$};
\node at (6,-2)[int] (AWGN) {AWGN Channel};
\node at (8.7,0)[int] (Demodulation) {Demodulation};
\node at (13,0)[int] (BER) {BER Meter};

\draw[->](Source) -- node {$\overline{u}$}(Modulation);
\draw[->](Modulation) -- node {$\overline{x}$}(Adder);
\draw[->](AWGN) -- node {$\overline{n}$}(Adder);
\draw[->](Adder) -- node {$\overline{y}$}(Demodulation);
\draw[->](Demodulation) -- node {$  \hat{\overline{u}}  $}(BER);

\draw (0.85,0) -- (0.85,1.5);
\draw (0.85,1.5) -- (13,1.5);
\draw [->] (13,1.5) -- node {}(BER);

\end{tikzpicture}
\end{figure}

\end{document}

A workaround to this would be to e.g. rotate the tikzpicture, which could be done by using rotatebox:

http://i.imgur.com/ov4yPTm.png?1

\documentclass[a4paper]{article}

\usepackage{tikz}
\usetikzlibrary{arrows}
\usepackage{verbatim}
\usepackage{showframe}% added to show that the figure is being centered

\begin{document}
\pagestyle{empty}

\begin{figure}    

\tikzstyle{int}=[draw,  minimum size=2em]
\tikzstyle{init} = [pin edge={to-,thick,black}]
\tikzset{  sum/.style      = {draw, circle, node distance = 2cm}}

\centering
\rotatebox{90}{%
\begin{tikzpicture}[node distance=3cm,auto,>=latex']
\node at (-1,0)[int] (Source) {Source};
\node at (3,0)[int] (Modulation) {Modulation - QAM};
\node at (6,0)[circle,draw] (Adder) {\Large$+$};
\node at (6,-2)[int] (AWGN) {AWGN Channel};
\node at (8.7,0)[int] (Demodulation) {Demodulation};
\node at (13,0)[int] (BER) {BER Meter};

\draw[->](Source) -- node {$\overline{u}$}(Modulation);
\draw[->](Modulation) -- node {$\overline{x}$}(Adder);
\draw[->](AWGN) -- node {$\overline{n}$}(Adder);
\draw[->](Adder) -- node {$\overline{y}$}(Demodulation);
\draw[->](Demodulation) -- node {$  \hat{\overline{u}}  $}(BER);

\draw (0.85,0) -- (0.85,1.5);
\draw (0.85,1.5) -- (13,1.5);
\draw [->] (13,1.5) -- node {}(BER);

\end{tikzpicture}
}
\end{figure}

\end{document}

You could also try rescaling the entire picture. Considering you want the picture to be centered, the underlying implication would be that the picture should have a width less than the horizontal typeset width. Using the transform shape key makes sure the scale key also affects your node size. I used scale=.7 in the example, but this value can of course be adjusted to your needs:

http://i.imgur.com/FifmxV8.png?1

\documentclass[a4paper]{article}

\usepackage{tikz}
\usetikzlibrary{arrows}
\usepackage{verbatim}
\usepackage{showframe}% added to show that the figure is being centered

\begin{document}
\pagestyle{empty}

\begin{figure}    

\tikzstyle{int}=[draw,  minimum size=2em]
\tikzstyle{init} = [pin edge={to-,thick,black}]
\tikzset{  sum/.style      = {draw, circle, node distance = 2cm}}

\centering
\begin{tikzpicture}[node distance=3cm,auto,>=latex',scale=.7,transform shape]
\node at (-1,0)[int] (Source) {Source};
\node at (3,0)[int] (Modulation) {Modulation - QAM};
\node at (6,0)[circle,draw] (Adder) {\Large$+$};
\node at (6,-2)[int] (AWGN) {AWGN Channel};
\node at (8.7,0)[int] (Demodulation) {Demodulation};
\node at (13,0)[int] (BER) {BER Meter};

\draw[->](Source) -- node {$\overline{u}$}(Modulation);
\draw[->](Modulation) -- node {$\overline{x}$}(Adder);
\draw[->](AWGN) -- node {$\overline{n}$}(Adder);
\draw[->](Adder) -- node {$\overline{y}$}(Demodulation);
\draw[->](Demodulation) -- node {$  \hat{\overline{u}}  $}(BER);

\draw (0.85,0) -- (0.85,1.5);
\draw (0.85,1.5) -- (13,1.5);
\draw [->] (13,1.5) -- node {}(BER);
\end{tikzpicture}
\end{figure}

\end{document}
3
  • You could just add rotate=90,every node/.style={rotate=90} in the tikzpicture options, you know. Oct 20, 2014 at 15:34
  • 1
    @SeanAllred Thank you for mentioning this and of course thank you for contributing to the list of options available. I excluded this solution because it messes with the spacing of the nodes. Fixing this leads to a substantially more complex solution. Since I'm unaware of the background of the person that made the request, I believe a solution with increased complexity will only be detrimental to his or her understanding of the options available for such a simple task. Feel free to persuade me to include it anyway. :-)
    – 1010011010
    Oct 20, 2014 at 16:06
  • Oh, not at all! I wasn't aware that anomalies would occur in spacing. Flooding someone with information is never truly helpful. :) Oct 20, 2014 at 16:09

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.