TransWikia.com

How to center a tikz picture in a latex beamer page

TeX - LaTeX Asked on February 22, 2021

How to center the tikzpicture? As this now, it seems to start from the left margin.

Slide

Here is my code:

documentclass{beamer}

% choose theme (warning, to use the UniBern theme you need to copy the files beamerthemeUniBern.sty and ublogo.pdf in the same directory)
usetheme{theme1}

% packages

usepackage{graphicx}
usepackage{hyperref} % allows clickable urls
usepackage{tikz}
usepackage{listings} % show code
usepackage{dsfont} % show code
usepackage{fontawesome}
usepackage{tikz}
usetikzlibrary{snakes}
usetikzlibrary{positioning}
newlengthyearposx
makeatletter
defblfootnote{gdef@thefnmark{}@footnotetext}
makeatother

definecolor{lightgrey}{RGB}{245,245,245}

% slide numbering
setbeamertemplate{sidebar right}{}
setbeamertemplate{footline}{%
    hfillusebeamertemplate***{navigation symbols}
    hspace{1cm}insertframenumber{}/inserttotalframenumber}

% define title page

% begin document
begin{document}


    

frame{
    frametitle{a}
    %till 2000

scalebox{0.7}{
begin{tikzpicture}[
node distance=1.5 cm and 0.8cm,
ar/.style={->,>=latex,thick},
mynode/.style={
  draw,
  line width=1pt,
  text width=3cm,
  minimum height= 1 cm,
  align=center
  }
]

node[mynode,text width=3cm, fill=lightgrey] (c1) at (0,0) {Diagnosed};
node[mynode,text width=3.5cm, fill=lightgrey, below=1cm of c1] (c2) {blablabla [1cm]$  $};
node[mynode,text width=3.5cm, fill=lightgrey, below=1cm of c2] (c3) {blablabla [1cm]$  $};

node[align=center,text width=3cm,left=2cm of c2] (c2_3) {First-line  blbla};
node[align=center,text width=3cm,left=2cm of c3] (c3_3) {Second-line  blbla};
node[draw,align=center,text width=18cm,below left =1cm and -11cm of c3] (text1) {textbf{aaa}: Very long long long text 1 hspace{0.5cm}
textbf{aaa}: Very long long long text 2hspace{0.5cm}
textbf{aaa}: Very long long long text 3};

draw[->,thick] (c1) -- (c2);
draw[->,thick] (c2) -- (c3);
end{tikzpicture}}
}
end{document}

Here is the theme:

RequirePackage{calc}
RequirePackage{etoolbox}

mode<presentation>

newdimenbeamer@ubBorderHeight
newdimenbeamer@ubTitleHeight
newdimenbeamer@ubFooterHeight
newdimenbeamer@ubOverlap

beamer@ubOverlap=0.2pt
beamer@ubBorderHeight=0.015paperheight
beamer@ubTitleHeight=0.125paperheight
beamer@ubFooterHeight=0.032paperheight

definecolor{ubRed}{HTML}{E6002E}
definecolor{ubGrey}{RGB}{255,255,255}

ProcessOptionsBeamer

setbeamercolor{normal text}{fg=black}
setbeamercolor{alerted text}{fg=ubRed}
setbeamercolor{itemize item}{fg=ubRed}
setbeamercolor{itemize subitem}{fg=ubRed}
setbeamercolor{example text}{fg=black}
setbeamercolor{structure}{fg=black}
setbeamercolor{date in title page}{fg=black}

% set fonts
RequirePackage[scaled]{helvet}
renewcommand*familydefault{sfdefault}

usefonttheme[onlymath]{serif}
RequirePackage{iftex}
ifPDFTeX
RequirePackage[T1]{fontenc}
RequirePackage[utf8]{inputenc}
fi



setbeamerfont*{structure}{series=bfseries}
setbeamerfont*{title}{size*={18}{22}}
setbeamercolor*{title}{bg=ubRed}
setbeamerfont*{subtitle}{size*={18}{22}}
setbeamerfont*{author}{size*={10}{12},series=bfseries}
setbeamerfont*{institute}{size*={10}{12}}
setbeamerfont*{frametitle}{size*={18}{22}}
setbeamercolor*{frametitle}{fg=ubRed}
setbeamerfont*{framesubtitle}{size*={18}{22}}
setbeamercolor*{framesubtitle}{fg=black}
setbeamerfont*{date}{size*={8}{10}}
setbeamerfont*{headline}{size*={8}{10}}
beamertemplatenavigationsymbolsempty

defbeamertemplate*{title page}{uni bern}{%
  % list trick from http://www.tex.ac.uk/cgi-bin/texfaq2html?label=chngmargonfly
  begin{list}{}{
  setlength{listparindent}{parindent}%
  setlength{itemindent}{parindent}%
  setlength{parsep}{parskip}%
  setlength{leftmargin}{0pt}%
  setlength{rightmargin}{0pt}}
  item[]
  vspace*{stretch{2}}
  {usebeamerfont*{title}color{ubRed}inserttitlepar} % no idea why usebeamercolor{title} does not work here
  {usebeamerfont*{subtitle}insertsubtitlepar}
  vspace{stretch{1}}
  {usebeamerfont{author}insertauthorpar}
  {usebeamerfont{date}insertdatepar}
  vspace*{stretch{2}}
  end{list}
%  end{minipage}
}

defbeamertemplate*{background canvas}{uni bern}{%
begin{minipage}[t]{paperwidth+10pt}
   setlength{baselineskip}{0pt}
   setlength{lineskip}{-beamer@ubOverlap}
   setlength{parskip}{0pt}
   ifnumgreater{c@framenumber}{1}{}{}

   ifnumgreater{c@framenumber}{2}{
        ifdefempty{insertframesubtitle}{
          rule{0pt}{6ex+beamer@ubTitleHeight}
          color{ubGrey}rule{paperwidth}{paperheight-beamer@ubTitleHeight-beamer@ubFooterHeight-6ex}
        }{
          rule{0pt}{6ex+2beamer@ubTitleHeight}          
          color{ubGrey}rule{paperwidth}{paperheight-2beamer@ubTitleHeight-beamer@ubFooterHeight-6ex}   
        }
   }{}
  end{minipage}
}

defbeamertemplate*{headline}{uni bern}{% 
    ifnumgreater{c@framenumber}{2}{
      vspace{3.5ex}
      hspace{4.5ex}inserttitle
    }{}
}

defbeamertemplate*{frametitle}{uni bern}{
begin{minipage}[b]{textwidth}
vspace{0.5ex}
raggedright
vspace{3.5ex}
{insertframetitlepar}
{usebeamerfont{framesubtitle}usebeamercolor*{framesubtitle}insertframesubtitle}
end{minipage}
}


% set bullets

defbeamertemplate*{itemize item}{uni bern}{$bullet$}
defbeamertemplate*{itemize subitem}{uni bern}{$bullet$}
setbeamersize{text margin left=4ex}
setbeamersize{text margin right=4ex}



mode<all>

One Answer

If you want to center something that exceeds the textwidth (and therefore will not be centered but left-aligned), it usually helps to pretend that the something has width zero.

begin{center}
makebox[0pt]{%
   something%
}
end{center}

Make sure that there are no spurious spaces, since these would bring the something out of center.

enter image description here

documentclass{beamer}

% choose theme (warning, to use the UniBern theme you need to copy the files beamerthemeUniBern.sty and ublogo.pdf in the same directory)
usetheme{theme1}

% packages

usepackage{graphicx}
usepackage{hyperref} % allows clickable urls
usepackage{tikz}
usepackage{listings} % show code
usepackage{dsfont} % show code
usepackage{fontawesome}
usepackage{tikz}
usetikzlibrary{snakes}
usetikzlibrary{positioning}
newlengthyearposx
makeatletter
defblfootnote{gdef@thefnmark{}@footnotetext}
makeatother

definecolor{lightgrey}{RGB}{245,245,245}

% slide numbering
setbeamertemplate{sidebar right}{}
setbeamertemplate{footline}{%
    hfillusebeamertemplate***{navigation symbols}
    hspace{1cm}insertframenumber{}/inserttotalframenumber}

% define title page

% begin document
begin{document}


    

frame{
    frametitle{a}
    %till 2000

    begin{center}
      makebox[0pt]{%
scalebox{0.7}{%
begin{tikzpicture}[
node distance=1.5 cm and 0.8cm,
ar/.style={->,>=latex,thick},
mynode/.style={
  draw,
  line width=1pt,
  text width=3cm,
  minimum height= 1 cm,
  align=center
  }
]

node[mynode,text width=3cm, fill=lightgrey] (c1) at (0,0) {Diagnosed};
node[mynode,text width=3.5cm, fill=lightgrey, below=1cm of c1] (c2) {blablabla [1cm]$  $};
node[mynode,text width=3.5cm, fill=lightgrey, below=1cm of c2] (c3) {blablabla [1cm]$  $};

node[align=center,text width=3cm,left=2cm of c2] (c2_3) {First-line  blbla};
node[align=center,text width=3cm,left=2cm of c3] (c3_3) {Second-line  blbla};
node[draw,align=center,text width=18cm,below left =1cm and -11cm of c3] (text1) {textbf{aaa}: Very long long long text 1 hspace{0.5cm}
textbf{aaa}: Very long long long text 2hspace{0.5cm}
textbf{aaa}: Very long long long text 3};

draw[->,thick] (c1) -- (c2);
draw[->,thick] (c2) -- (c3);
end{tikzpicture}}}
end{center}
}
end{document}

Correct answer by gernot on February 22, 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