9

What do I do to shift this line to the right or above without changing the co-ordinates?

Enter image description here

\documentclass{beamer}
\usepackage{tikz}
\begin{document}
\begin{frame}

\tikzset{every picture/.style={line width=0.75pt}} % Set default line width to 0.75pt

\begin{tikzpicture}[x=0.5pt,y=0.5pt,yscale=-1,xscale=1]
%uncomment if require: \path (0,390); % Set diagram left start at 0, and has height of 390

\draw    (257.75,206) -- (398.5,205) ;

\end{tikzpicture}

\end{frame}
\end{document}

Edit: according to @Max Snippe

\documentclass{beamer}
\usepackage{tikz}

\begin{document}
\begin{frame}

\tikzset{every picture/.style={line width=0.75pt}} % Set default line width to 0.75pt

\begin{tikzpicture}[x=0.5pt,y=0.5pt,yscale=-1,xscale=1]
%uncomment if require: \path (0,390); % Set diagram left start at 0, and has height of 390

\begin{scope}[xshift={(5,0)}]

\draw    (257.75,206) -- (398.5,205) ;

\end{scope}
\end{tikzpicture}

\end{frame}
\end{document}
10
  • you can use xshift and yshift before the coordinates: \draw [xshift=5pt,yshift=2pt](257.75,206) -- [xshift=5pt,yshift=2pt](398.5,205) ;
    – sporc
    Jul 19, 2018 at 7:12
  • i want to do this as a whole. like if there were 100 lines, i want to shift all of them together using a single xshift/yshift command
    – afsara_ben
    Jul 19, 2018 at 7:19
  • 3
    You can enclose your lines in a \begin{scope}[shift={(<x>,<y>)}] end \end{scope}. Note that you need the braces surrounding the coordinates in the argument of shift because otherwise the , will be seen as a key delimiter.
    – Max
    Jul 19, 2018 at 7:24
  • 1
    The coordinates inside a tikzpicture is only relative to other things in that tikzpicture, not the frame/page. So if you have one tikzpicture that contains just \draw (0,0) -- (1,1); and another one that contains just \draw (120,0) -- (121,1);, the two will look identical. As far as TeX is concerned, it just sees a box that is 1cm by 1cm. The way that box is positioned on the page is exactly the same as the way the letter X is positioned on the page, or an \includegraphics. Jul 19, 2018 at 7:44
  • 1
    @afsara_ben If you consider one of the given answers sufficient, you can accept it by clicking the light-green check mark (✔) beside it, right underneath the votes. Note that this does not have to be the answer with the highest score, but rather the answer that you deem the best solution. I saw that you have not yet accepted any answer to any of your questions. Please consider doing so (this also grants you a badge).
    – Max
    Jul 19, 2018 at 13:07

3 Answers 3

14

As already pointed out by @marmot and @TorbjørnT., the coordinates you use in your tikzpicture environment are relative, and not absolute with respect to the page. There are a few ways to move the line with respect to the page.

Also in your M(N)WE you use xshift={(<x>,<y>)}, but xshift takes a dimension as argument. You should use shift={(<x>,<y>)} where you can set <y> to 0, or use xshift=<x>.

You could draw an invisible path that outlines approximately the entire page such that you can draw inside that path.

\documentclass{beamer}
\usepackage{tikz}

\tikzset{every picture/.style={line width=0.75pt}} %set default line width to 0.75pt        

\begin{document}
    \begin{frame}

        \begin{tikzpicture}[x=0.5pt,y=0.5pt,yscale=-1,xscale=1,overlay]
            \path (0,0) rectangle (600,390); % Use this to set the dimensions to approximately the page size

            \draw[help lines,step=20] (0,0) grid (600,390); % Optional grid

            \draw[red]    (257.75,206) -- (398.5,205) ;

            \begin{scope}[shift={({50*cos(60)},{50*sin(60)})}]

                \draw[red]    (257.75,206) -- (398.5,205) ;

            \end{scope}
        \end{tikzpicture}

    \end{frame}
\end{document}

This GIF shows multiple such displacements.

enter image description here

Without the grid this would be:

enter image description here

An other way could be to use the overlay option and draw everything within a scope that is shifted with respect to a page coordinate.

\documentclass{beamer}
\usepackage{tikz}

\tikzset{every picture/.style={line width=0.75pt}} %set default line width to 0.75pt        

\begin{document}
    \begin{frame}

        \begin{tikzpicture}[x=0.5pt,y=0.5pt,yscale=-1,xscale=1,overlay]
%            \path (0,0) rectangle (600,390); % Use this to set the dimensions to approximately the page size
%        
%            \draw[help lines,step=20] (0,0) grid (600,390); % Optional grid
%        
%            \draw    (257.75,206) -- (398.5,205) ;
            \begin{scope}[shift={(current page.south west)}]
                \begin{scope}[shift={({50*cos(60)},{50*sin(60)})}]

                    \draw[]    (257.75,206) -- (398.5,205) ;

                \end{scope}
            \end{scope}
        \end{tikzpicture}

    \end{frame}
\end{document}

This way your origin (0,0) is located on the bottom left corner of the page.

enter image description here

6

The coordinates inside a tikzpicture is only relative to other things in that tikzpicture, not the frame/page. So if you have one tikzpicture that contains just \draw (0,0) -- (1,1); and another one that contains just \draw (120,0) -- (121,1);, the two will look identical. As far as TeX is concerned, it just sees a box that is 1cm by 1cm. The way that box is positioned on the page is exactly the same as the way the letter X is positioned on the page, or an \includegraphics.

(This applies to the default behaviour, Max's answer demonstrates one way of drawing things relative to the page.)

So you can move a tikzpicture horizontally the same way you move an image say. For example by adding \hspace{<dimension>} right before it (which, depending on the context, might not be the best method for horizontal positioning).

For the vertical position, you can use the baseline key in the tikzpicture option. What this does is define which y-coordinate, of the coordinates inside the tikzpicture, that is placed on the baseline of the surrounding text. In your case, with y=0.5pt,yscale=-1 and the y-coordinates of the line being about 205, baseline=-102.5pt will place the \drawn line itself on the baseline of the surrounding text. baseline=-120pt will move the tikzpicture up on the page, baseline=-80pt will move it down.

I suspect some of the other answers might be more useful for you, but I'll add this anyway.

Here is a frame with three different examples.

\documentclass{beamer}
\usepackage{tikz}
\begin{document}
\begin{frame}

\tikzset{every picture/.style={line width=0.75pt}} %set default line width to 0.75pt        

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% 1: \hspace{1cm}, baseline=-120pt
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
foo\hspace{1cm}\begin{tikzpicture}[x=0.5pt,y=0.5pt,yscale=-1,xscale=1,baseline=-120pt]

\draw    (257.75,206) -- (398.5,205)coordinate(a) ;

\end{tikzpicture}bar


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% 2: \hspace{2cm}, baseline=-102.75pt
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

foo\hspace{2cm}\begin{tikzpicture}[x=0.5pt,y=0.5pt,yscale=-1,xscale=1,baseline=-102.75pt]
%uncomment if require:
% \path (0,390); %set diagram left start at 0, and has height of 390

\draw    (257.75,206) -- (398.5,205)coordinate(a) ;

\end{tikzpicture}bar


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% 3: \hspace{-1cm}, baseline=-80pt
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

foo\hspace{-1cm}\begin{tikzpicture}[x=0.5pt,y=0.5pt,yscale=-1,xscale=1,baseline=-80pt]
%uncomment if require:
% \path (0,390); %set diagram left start at 0, and has height of 390

\draw    (257.75,206) -- (398.5,205)coordinate(a) ;

\end{tikzpicture}bar

\end{frame}
\end{document}
4

TikZ always adds by default a bounding box that fits all elements of the picture. Therefore, as long as you only draw the line, the absolute coordinates are unimportant (but of course the relative ones are). However, at the very moment you add an invisible point relative to the line, the bounding box will adjust and the line will move.

\documentclass{beamer}
\usepackage{tikz}
\begin{document}
\foreach \X in {0,2,...,160}
{\begin{frame}{I'm floating ;-)}
\tikzset{every picture/.style={line width=0.75pt}} %set default line width to 0.75pt        

\begin{tikzpicture}[x=0.5pt,y=0.5pt,yscale=-1,xscale=1]
%uncomment if require: \path (0,390); %set diagram left start at 0, and has height of 390

\draw    (257.75,206) -- (398.5,205) ;
\path (257.75,206) -- ++ (-\X,0);
\end{tikzpicture}
\end{frame}
}
\end{document}

enter image description here

You must log in to answer this question.

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