1

When using \centering this way, I get everything centered, text and figure:

\documentclass[varwidth=true,border=5pt]{standalone}
\usepackage{color}
\usepackage{tikz}

\newenvironment{nscenter}
  {\parskip=0pt\par\nopagebreak\centering}
  {\par\noindent\ignorespacesafterend}
\begin{document}
\centering
Generations lived here alone. Strangers this is our home! Strangers, this is our home! Welcome strangers, stay as our friends. The beauty of our land never ends.
\newline 
\newline 
\begin{tikzpicture}
  \filldraw[orange] (-1.2, 0) circle  (5pt);
  \filldraw[blue]   ( 2  , 0) circle  (2pt);
  \draw             ( 0  , 0) ellipse (2 and 1);
\end{tikzpicture}
\end{document}

But this does not center anything:

\documentclass[varwidth=true,border=5pt]{standalone}
\usepackage{color}
\usepackage{tikz}

\newenvironment{nscenter}
  {\parskip=0pt\par\nopagebreak\centering}
  {\par\noindent\ignorespacesafterend}
\begin{document}
Generations lived here alone. Strangers this is our home! Strangers, this is our home! Welcome strangers, stay as our friends. The beauty of our land never ends.    \newline 
\newline 
\begin{tikzpicture}
  \centering
  \filldraw[orange] (-1.2, 0) circle  (5pt);
  \filldraw[blue]   ( 2  , 0) circle  (2pt);
  \draw             ( 0  , 0) ellipse (2 and 1);
\end{tikzpicture}
\end{document}

The use of domain \begin{center}...\end{center} did not work either. Have gone through the other questions: scaling, \par, frames, etc., ...nothing.

Update: I try to center the figure with respect to the rest of the text in the simplest way possible (minimal latex writing and packages imported).

Note: Text on the document updated, just to show that the text means a width larger than the image.

  • 4
    Why would you expect that a \centering inside a tikzpicture centers the diagram? – Torbjørn T. Apr 5 '17 at 17:48
  • I've seen it used this way somewhere, but may be it wasn't a good idea... – nightcod3r Apr 5 '17 at 17:52
  • Here it is just ignored. What exactly are you trying to center and in relation to what? – daleif Apr 5 '17 at 17:55
  • 1
    Unable to reproduce problem, using pdflatex. When the varwidth option is used, the drawing (which is largest) is automatically ccentered, whether or not you specify centering. When varwidth is not use, nothing is centered, in either case. – user103221 Apr 5 '17 at 17:55
  • @daleif I try to center the figure with respect to the rest of the text in the simplest way possible (minimal latex writing and packages imported). – nightcod3r Apr 5 '17 at 17:57
4

The most natural way to centre something is the center environment

enter image description here

\documentclass{article}
\usepackage{tikz}
\begin{document}

\hrule

\begin{center}
\begin{tikzpicture}
  \filldraw[orange] (-1.2, 0) circle  (5pt);
  \filldraw[blue]   ( 2  , 0) circle  (2pt);
  \draw             ( 0  , 0) ellipse (2 and 1);
\end{tikzpicture}
\end{center}

\hrule

\end{document}

Or with standalone:

enter image description here

\documentclass[varwidth=true,border=5pt]{standalone}
\usepackage{color}
\usepackage{tikz}


\begin{document}
This is our home
\newline% bad use makes underfull box 
\newline % bad use makes underfull box 

\begin{center}
\begin{tikzpicture}
  \filldraw[orange] (-1.2, 0) circle  (5pt);
  \filldraw[blue]   ( 2  , 0) circle  (2pt);
  \draw             ( 0  , 0) ellipse (2 and 1);
\end{tikzpicture}
\end{center}
\end{document}

or with standalone and the longer text in the edited question

enter image description here

  • For some extrange reason, that solution is not working if I just add the \begin{center}...\end{center} environment to the text in the question (2nd one) and delete the \centering command. – nightcod3r Apr 5 '17 at 18:54
  • 1
    @nightcod3r well obviously it can not work in standalone as you are there you are making the page as wide as the picture so there is nothing to centre. – David Carlisle Apr 5 '17 at 19:11
  • Could you please update the figure with the new text in the question? Just to show that it means a wider page. – nightcod3r Apr 5 '17 at 19:21
  • I'm trying to figure it out... cause this is not what I get. The file is rendered with this command: texfot --quiet --interactive pdflatex -shell-escape file.tex > /dev/null 2>&1 && \ – nightcod3r Apr 5 '17 at 19:35
  • @nightcod3r I have no idea what texfot is, the above is made with an up to date texlive 2016 pdflatex. – David Carlisle Apr 5 '17 at 20:10

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.