2

I am writing a document and I am trying to put a tikz picture centered as a subfigure. Altought the seems to have a proper size (I used a fbox to check), the image is not being centered in the midle of the column. What is happening here? I have read something about an invisible node taking up space on tikzpictures of other people but I think this is not my case here.

Below the TEX files that I use. I am using the ´showframe´ package to check if the picture overflows fto the margins and a ´fbox´ to see if its too big. Please do note that I am using the ´subfig´ package, instead of ´subcaption´, as I already found it defined in an IEEEtrans template and I am not sure if its possible to change it.

I would appreciate any help in this! :)

MWE of the document:

\documentclass[conference,a4paper]{IEEEtran}
\usepackage{cite}
\usepackage{amsmath,amssymb,amsfonts}
\interdisplaylinepenalty=2500
\usepackage{algorithmic}
\usepackage{textcomp}
\usepackage{xcolor}
\def\BibTeX{{\rm B\kern-.05em{\sc i\kern-.025em b}\kern-.08em
   T\kern-.1667em\lower.7ex\hbox{E}\kern-.125emX}}

%- Supporting packages
\usepackage{lipsum}
\usepackage{showframe}

%- Figures and subfigures packages
\usepackage[caption=false,font=footnotesize]{subfig}
\usepackage[pdftex]{graphicx}

%- Last package to write, as it loads a bunch of them, an it could give clash problems
\usepackage{pgfplotstable} % Loads a bunch of packages
\pgfplotsset{compat=newest}

%- Loading of libraries from  pre-loaed packages
\usetikzlibrary{math,% For computations. Variables must be declared in a tikzmath environment but can be used outside
                       decorations.markings,       % For arrow tips
                      decorations.pathreplacing, % For brackets on image
                      calligraphy,}                        % For brackets on image
\tikzstyle{help lines}=[thin,gray!40]


\begin{document}

\title{A title
}

\author{Jes
       \\
   \IEEEauthorblockA{LaTeX is not easy}
   }
\maketitle

\begin{abstract}
\lipsum[1]
\end{abstract}

\begin{IEEEkeywords}
   Ipsum lipsum diggity tippity.
\end{IEEEkeywords}


%- Sections
\section{Introduction}
\lipsum[1-3]

\begin{figure}[h]
   \centering
   \subfloat[]{\fbox{
           \input{Tikzpicture.tex}%
   }}
   \vfil
   \subfloat[]{
       \def\svgwidth{3.15in}
       \input{Tikzpicture.tex}%
   }
   
   \caption{This is a caption}
   
   \label{fig:IndoorLocSketch}
\end{figure}

\lipsum[1]

\end{document}

And here the code used for the Tikzpicture:

%- Define colors to be used
\definecolor{Color Lens}{rgb}{0, 1, 0.5}            % Color for lens
\definecolor{Color FSS}{rgb}{1.0, 0.49, 0.0}        % Color FSS
\definecolor{Color IncWave}{rgb}{0,0,1}             % Color incoming wave
\definecolor{Color SurfRefl}{rgb}{0.0, 0.72, 0.92}  % Color surface refraction
\definecolor{Color ResFreq}{rgb}{1.0, 0.49, 0.0}    % Color f0
\definecolor{Color FocalPoint}{rgb}{0.03, 0.47, 0.19}

%\begin{document}
    \tikzmath{
        \LensCenterX = 6;
        \LensCenterY =  0;
        \LensRadius  = 1.75; % In cm
        \FSSThick    = 1;    % In mm
        \ArrowPos    = (2/3)*\LensRadius;
        \ArrowThick  = 0.3; % In mm
    } 

    \begin{tikzpicture}[scale=0.43,every node/.style={scale=0.9}]
        %- Help grid
        %\draw [help lines, step=1] (-12,-5) grid (11,5);
        
        %- Lens
        \draw [color = Color Lens, fill=Color Lens]
            (\LensCenterX,\LensCenterY) circle (\LensRadius cm);
        
        %- Inner lens drawing
        \draw[color = Color FSS,line width=\ArrowThick mm,-stealth]
        (\LensCenterX-1,\ArrowPos) --  (\LensCenterX+1,0.33) node [midway, below]{$f_0$};
        \draw[color = Color FSS,line width=\ArrowThick mm,-stealth]
        (\LensCenterX+1,-0.33) --  (\LensCenterX-1,-\ArrowPos);
        
        %- Lens focal point
        \draw [color = Color FocalPoint, fill = Color FocalPoint]
        (\LensCenterX+1.3,0) circle (0.33 cm);
        
        %- Metallic backside
        \draw [color=Color FSS,line width=\FSSThick mm] 
            (\LensCenterX,\LensRadius) arc (-90:90:-\LensRadius cm);       % Backside metallization
        
        %- FSS
        \draw [color=black,line width=\FSSThick mm] 
            (\LensCenterX,-\LensRadius) arc (90:270:-\LensRadius cm);      % FSS
            
        %- Annotate FSS
        \node [anchor=west,color=Color FSS,inner sep=0,outer sep=0] (FSS) at (\LensCenterX-\LensRadius-0.1,2.3) {FSS (Passband)};
        
        %- Annotate lens
        \draw[-stealth,line width=\ArrowThick mm] (\LensCenterX-2,-2.5) 
            node [anchor = north] {Lens} -- (\LensCenterX,-\LensRadius+0.3);    
            
        %- Annotate metal
        \draw[-stealth,line width=\ArrowThick mm] (\LensCenterX-0.25,-2.5)
            node [anchor = north] {Metal} -- (\LensCenterX+0.5,-1.9);
        %- Annotate Retroreflector
        \draw[line width=\ArrowThick mm, decoration={calligraphic brace,mirror,raise=-1pt,amplitude=10pt},decorate]
        (3,-3.25) -- node[below=6pt] {Retroreflector} (6.7,-3.25);
            
        %- Annotate focal point
        \draw[-stealth,line width=\ArrowThick mm] (8,-1.5) 
            node [anchor = north,align = left,inner sep=0,outer sep=0] {Focal \\ point} -- (\LensCenterX+1.4,-0.2);     
                    
        %- Arrows
        \draw[stealth-,line width=\ArrowThick mm, color = Color IncWave]
            (\LensCenterX-\LensRadius-0.5,\ArrowPos) 
            --  (-\LensCenterX+2,\ArrowPos)
            node [anchor=south west] {\color{black} Incoming wave};
            
        \draw[-stealth,line width=\ArrowThick mm, color = Color SurfRefl] 
            (\LensCenterX-\LensRadius-0.5,0) 
            node [anchor=south east] {\color{black} Surface reflection (no $f_0$) }
            -- (-\LensCenterX+2,0);
            
        \draw[-stealth,line width=\ArrowThick mm, color = Color ResFreq] 
            (\LensCenterX-\LensRadius-0.5,-\ArrowPos) 
            node [anchor=south east] {\color{black} $f_0$ (Delayed)} 
            --  (-\LensCenterX+2,-\ArrowPos);
            
        %- Reader text
        \node[anchor = center] (Reader) at (-7.25,-1) {Reader};
        %- Reader box
        \draw [line width=\ArrowThick mm] (-8.5,-0.25) rectangle +(2.5,-1.5);
        %- Reader antenna
        \draw [line width=\ArrowThick mm] (-6,-1) -- ++(0.75,0) -- ++(0,1.6) --++(0,-0.8) --++ (0.35,0.8) --++ (-0.35,-0.8) --++ (-0.35,0.8);
        
    \end{tikzpicture}

1 Answer 1

2

The reason for the displacement is simple: lots of unintended spaces. It starts in the main file. At all the marked places, the % is necessary in order to avoid a spurious space.

   \subfloat[]{\fbox{% <<< HERE
           \input{Tikzpicture.tex}% <<< HERE
   }}
   \vfil
   \subfloat[]{% <<< HERE
       \def\svgwidth{3.15in}% <<< HERE
       \input{Tikzpicture.tex}% <<< HERE
   }

It gets really bad in Tikzpicture.tex.

\definecolor{Color Lens}{rgb}{0, 1, 0.5}% <<< HERE           % Color for lens
\definecolor{Color FSS}{rgb}{1.0, 0.49, 0.0}% <<< HERE       % Color FSS
\definecolor{Color IncWave}{rgb}{0,0,1}% <<< HERE            % Color incoming wave
\definecolor{Color SurfRefl}{rgb}{0.0, 0.72, 0.92}% <<< HERE % Color surface refraction
\definecolor{Color ResFreq}{rgb}{1.0, 0.49, 0.0}% <<< HERE   % Color f0
\definecolor{Color FocalPoint}{rgb}{0.03, 0.47, 0.19}% <<< HERE
% <<< HERE
%\begin{document}
    \tikzmath{
        \LensCenterX = 6;
        \LensCenterY =  0;
        \LensRadius  = 1.75; % In cm
        \FSSThick    = 1;    % In mm
        \ArrowPos    = (2/3)*\LensRadius;
        \ArrowThick  = 0.3; % In mm
    }% <<< HERE
% <<< HERE
    \begin{tikzpicture}[scale=0.43,every node/.style={scale=0.9}]

enter image description here

main.tex

\documentclass[conference,a4paper]{IEEEtran}
\usepackage{cite}
\usepackage{amsmath,amssymb,amsfonts}
\interdisplaylinepenalty=2500
\usepackage{algorithmic}
\usepackage{textcomp}
\usepackage{xcolor}
\def\BibTeX{{\rm B\kern-.05em{\sc i\kern-.025em b}\kern-.08em
   T\kern-.1667em\lower.7ex\hbox{E}\kern-.125emX}}

%- Supporting packages
\usepackage{lipsum}
\usepackage{showframe}

%- Figures and subfigures packages
\usepackage[caption=false,font=footnotesize]{subfig}
\usepackage[pdftex]{graphicx}

%- Last package to write, as it loads a bunch of them, an it could give clash problems
\usepackage{pgfplotstable} % Loads a bunch of packages
\pgfplotsset{compat=newest}

%- Loading of libraries from  pre-loaed packages
\usetikzlibrary{math,% For computations. Variables must be declared in a tikzmath environment but can be used outside
                       decorations.markings,       % For arrow tips
                      decorations.pathreplacing, % For brackets on image
                      calligraphy,}                        % For brackets on image
\tikzstyle{help lines}=[thin,gray!40]


\begin{document}

\title{A title
}

\author{Jes
       \\
   \IEEEauthorblockA{LaTeX is not easy}
   }
\maketitle

\begin{abstract}
\lipsum[1]
\end{abstract}

\begin{IEEEkeywords}
   Ipsum lipsum diggity tippity.
\end{IEEEkeywords}


%- Sections
\section{Introduction}
\lipsum[1-3]

\begin{figure}[h]
   \centering
   \subfloat[]{\fbox{%
           \input{Tikzpicture.tex}%
   }}
   \vfil
   \subfloat[]{%
       \def\svgwidth{3.15in}%
       \input{Tikzpicture.tex}%
   }
   
   \caption{This is a caption}
   
   \label{fig:IndoorLocSketch}
\end{figure}

\lipsum[1]

\end{document}

Tikzpicture.tex

%- Define colors to be used
\definecolor{Color Lens}{rgb}{0, 1, 0.5}%            % Color for lens
\definecolor{Color FSS}{rgb}{1.0, 0.49, 0.0}%        % Color FSS
\definecolor{Color IncWave}{rgb}{0,0,1}%             % Color incoming wave
\definecolor{Color SurfRefl}{rgb}{0.0, 0.72, 0.92}%  % Color surface refraction
\definecolor{Color ResFreq}{rgb}{1.0, 0.49, 0.0}%    % Color f0
\definecolor{Color FocalPoint}{rgb}{0.03, 0.47, 0.19}%
%
%\begin{document}
    \tikzmath{
        \LensCenterX = 6;
        \LensCenterY =  0;
        \LensRadius  = 1.75; % In cm
        \FSSThick    = 1;    % In mm
        \ArrowPos    = (2/3)*\LensRadius;
        \ArrowThick  = 0.3; % In mm
    }% 
%
    \begin{tikzpicture}[scale=0.43,every node/.style={scale=0.9}]
        %- Help grid
        %\draw [help lines, step=1] (-12,-5) grid (11,5);
        
        %- Lens
        \draw [color = Color Lens, fill=Color Lens]
            (\LensCenterX,\LensCenterY) circle (\LensRadius cm);
        
        %- Inner lens drawing
        \draw[color = Color FSS,line width=\ArrowThick mm,-stealth]
        (\LensCenterX-1,\ArrowPos) --  (\LensCenterX+1,0.33) node [midway, below]{$f_0$};
        \draw[color = Color FSS,line width=\ArrowThick mm,-stealth]
        (\LensCenterX+1,-0.33) --  (\LensCenterX-1,-\ArrowPos);
        
        %- Lens focal point
        \draw [color = Color FocalPoint, fill = Color FocalPoint]
        (\LensCenterX+1.3,0) circle (0.33 cm);
        
        %- Metallic backside
        \draw [color=Color FSS,line width=\FSSThick mm] 
            (\LensCenterX,\LensRadius) arc (-90:90:-\LensRadius cm);       % Backside metallization
        
        %- FSS
        \draw [color=black,line width=\FSSThick mm] 
            (\LensCenterX,-\LensRadius) arc (90:270:-\LensRadius cm);      % FSS
            
        %- Annotate FSS
        \node [anchor=west,color=Color FSS,inner sep=0,outer sep=0] (FSS) at (\LensCenterX-\LensRadius-0.1,2.3) {FSS (Passband)};
        
        %- Annotate lens
        \draw[-stealth,line width=\ArrowThick mm] (\LensCenterX-2,-2.5) 
            node [anchor = north] {Lens} -- (\LensCenterX,-\LensRadius+0.3);    
            
        %- Annotate metal
        \draw[-stealth,line width=\ArrowThick mm] (\LensCenterX-0.25,-2.5)
            node [anchor = north] {Metal} -- (\LensCenterX+0.5,-1.9);
        %- Annotate Retroreflector
        \draw[line width=\ArrowThick mm, decoration={calligraphic brace,mirror,raise=-1pt,amplitude=10pt},decorate]
        (3,-3.25) -- node[below=6pt] {Retroreflector} (6.7,-3.25);
            
        %- Annotate focal point
        \draw[-stealth,line width=\ArrowThick mm] (8,-1.5) 
            node [anchor = north,align = left,inner sep=0,outer sep=0] {Focal \\ point} -- (\LensCenterX+1.4,-0.2);     
                    
        %- Arrows
        \draw[stealth-,line width=\ArrowThick mm, color = Color IncWave]
            (\LensCenterX-\LensRadius-0.5,\ArrowPos) 
            --  (-\LensCenterX+2,\ArrowPos)
            node [anchor=south west] {\color{black} Incoming wave};
            
        \draw[-stealth,line width=\ArrowThick mm, color = Color SurfRefl] 
            (\LensCenterX-\LensRadius-0.5,0) 
            node [anchor=south east] {\color{black} Surface reflection (no $f_0$) }
            -- (-\LensCenterX+2,0);
            
        \draw[-stealth,line width=\ArrowThick mm, color = Color ResFreq] 
            (\LensCenterX-\LensRadius-0.5,-\ArrowPos) 
            node [anchor=south east] {\color{black} $f_0$ (Delayed)} 
            --  (-\LensCenterX+2,-\ArrowPos);
            
        %- Reader text
        \node[anchor = center] (Reader) at (-7.25,-1) {Reader};
        %- Reader box
        \draw [line width=\ArrowThick mm] (-8.5,-0.25) rectangle +(2.5,-1.5);
        %- Reader antenna
        \draw [line width=\ArrowThick mm] (-6,-1) -- ++(0.75,0) -- ++(0,1.6) --++(0,-0.8) --++ (0.35,0.8) --++ (-0.35,-0.8) --++ (-0.35,0.8);
        
    \end{tikzpicture}%
3
  • OMG! Thanks for this. I did not know that spurious spaces are so critical in these things
    – Jes
    Jan 21, 2021 at 19:10
  • So basically, add a ´%´ in every line break, just in case :D
    – Jes
    Jan 21, 2021 at 19:12
  • @Jes If you want to be on the safe side, yes. If you overdo it, you will easily spot the missing space; the other way around is trickier.
    – gernot
    Jan 21, 2021 at 19:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .