TransWikia.com

Centering tikzpicture within figure won't work even after scaling

TeX - LaTeX Asked by Javier López on February 20, 2021

My code goes like this:

begin{figure}[H]
    centering
    begin{tikzpicture}[scale=.8]
    
    %*****************************************************************************************************************************
    % C Í R C U L O S

    tkzDefPoint(0,0){A}
    tkzDefPoint(6,0){B}
    
    tkzLabelPoints(A,B)
    tkzDrawPoints(A,B)
    
    tkzDrawCircle[R](A,3cm)
    tkzDrawCircle[R](B,1.5cm)

    path[name path = R1] (0,0) circle (3cm);
    path[name path = r1] (6,0) circle (1.5cm);
    
    path[name path = R2] (0,0) --++ (-3,-3);
    path[name path = r2] (6,0) --++ (-1.5,-1.5);

    %*****************************************************************************************************************************
    % R A D I O S
    
    path[name intersections={of=R1 and R2, by={R}}];
    path[name intersections={of=r1 and r2, by={r}}];
    
    draw[->] (0,0) -- (R) node[midway,sloped,above] {$R$};
    draw[->] (6,0) -- (r) node[midway,sloped,above] {$r$};
    
    %*****************************************************************************************************************************
    % D I S T A N C I A S
    
    draw[<->] (0,4) -- (6,4) node[midway,fill=white] {$D$};

    tkzExtSimilitudeCenter(A,3)(B,1.5)
    tkzGetPoint{I}

    tkzDefTangent[from with R=I](A,3cm)
    tkzGetPoints{C}{D}
    tkzDefTangent[from with R=I](B,1.5cm)
    tkzGetPoints{C'}{D'}

    draw (C) -- (C');
    draw (D) -- (D');

    end{tikzpicture}
    caption{}
    label{fig:massmann}
end{figure}

My tikzpicture was too wide, so I scaled it down to make it fit inside the document. However, what I get is this:

enter image description here

The figure name is the reference to know where the center is. As you can see, I’m using centering, but my figure is not centered. I tried begin{center}end{center}, but it didn’t work either. What can I do to center my figure?

Thanks in advance!

2 Answers

Adding

draw[red] (current bounding box.north east) -- (current bounding box.north west) -- (current bounding box.south west) -- (current bounding box.south east) -- cycle; 

into your tikzpicture environment reveals that the bounding box of the picture (red) is extends further to the right than the visible parts of the image, thus leaving the impression that the image is off center, while it actually is centered with respect to the textwidth (blue):

enter image description here

This is caused by tkzExtSimilitudeCenter(A,3)(B,1.5) which adds an invisible point right of the smaller circle.

To overcome this issue while keeping the image code (almost) the same, you can use the pgfinterruptboundingbox environment around the above mentioned command. This "hides" the invisible point from the bounding box calculation and results in the following output:

enter image description here

Full minimal working example:

documentclass{article}
usepackage{tkz-euclide}

begin{document}

begin{figure}
    centering
    begin{tikzpicture}[scale=0.8]
    
    %*****************************************************************************************************************************
    % C Í R C U L O S

    tkzDefPoint(0,0){A}
    tkzDefPoint(6,0){B}
    
    tkzLabelPoints(A,B)
    tkzDrawPoints(A,B)
    
    tkzDrawCircle[R](A,3cm)
    tkzDrawCircle[R](B,1.5cm)

    path[name path = R1] (0,0) circle (3cm);
    path[name path = r1] (6,0) circle (1.5cm);
    
    path[name path = R2] (0,0) --++ (-3,-3);
    path[name path = r2] (6,0) --++ (-1.5,-1.5);

    %*****************************************************************************************************************************
    % R A D I O S
    
    path[name intersections={of=R1 and R2, by={R}}];
    path[name intersections={of=r1 and r2, by={r}}];
    
    draw[->] (0,0) -- (R) node[midway,sloped,above] {$R$};
    draw[->] (6,0) -- (r) node[midway,sloped,above] {$r$};
    
    %*****************************************************************************************************************************
    % D I S T A N C I A S
    
    draw[<->] (0,4) -- (6,4) node[midway,fill=white] {$D$};
 




    begin{pgfinterruptboundingbox}
           tkzExtSimilitudeCenter(A,3)(B,1.5)
    end{pgfinterruptboundingbox}
        tkzGetPoint{I}
        tkzDefTangent[from with R=I](A,3cm)
    tkzGetPoints{C}{D}
    tkzDefTangent[from with R=I](B,1.5cm)
    tkzGetPoints{C'}{D'}

    draw (C) -- (C');
    draw (D) -- (D');
  
    end{tikzpicture}
    caption{}
    label{fig:massmann}
end{figure}


end{document}

Correct answer by leandriis on February 20, 2021

For fun, there's no problem making this figure with pstricks, and more specifically with pst-eucl, which defines commands to determine the common tangents of two circles:

 documentclass{article}
 usepackage{pst-eucl}

 begin{document}

    begin{figure}
    centering
    fbox{begin{pspicture}(-3,-3)(7.5,4.1)
    psset{arrowinset =0.12, dimen=inner}
    pstGeonode[PosAngle=-60](0,0){A}(6,0){B}
    pcline[offset=4cm]{<->}(A)(B)ncput*{$D$}
    pnodes(3;-135){R}(1.5;225){r}
    AplusB(B)(r){r}
    pstCircleOA{A}{R}pstCircleOA{B}{r}
    pstCircleExternalCommonTangent[PointSymbol=none, PointName=none, dimen=outer]{A}{R}{B}{r}{T1}{T2}{t1}{t2}
    pcline[offsetA=1pt, offsetB=1.5pt](T1)(t1)pcline[offsetA=-1pt, offsetB=-1.5pt](T2)(t2)
    psset{arrows=->,nrot=:D, labelsep=2pt}
    ncline{A}{R}nbput{$R$}%
    ncline{B}{r}nbput[nrot=:D]{$r$}%
    end{pspicture}}
    caption{A centred caption}
    end{figure}

 end{document} 

enter image description here

Answered by Bernard on February 20, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP