1

I have two very similar-looking polygons in different TikZ environments. Each polygon is symmetric across a horizontal line. I want to put them in one TikZ environment with the axes of symmetry aligned. I didn't know how to do this while keeping the titles of each figure in their respective positions.

\documentclass{amsart}
\usepackage{amsmath}
\usepackage{amsfonts}

\usepackage{tikz}
\usetikzlibrary{calc,angles,positioning,intersections}


\begin{document}

\begin{tikzpicture}

%A hexagon is drawn which is symmetric across the x-axis.
\coordinate (A) at (0,0);
\coordinate (O) at (-5,0);

\path[name path=x-axis] (A) -- (O);
\path[name path=horizontal_line_at_-1] (-5,-1) -- (0,-1);
\path[name path=horizontal_line_at_1] (-5,1) -- (0,1);

\coordinate (B) at ($(A) + (-135:2.5)$);
\draw (A) -- (B);

\coordinate (C') at ($(B) + (165:3)$);
\path[name path=extension_of_line_segment_BC] (B) -- (C');
\coordinate[name intersections={of=extension_of_line_segment_BC and horizontal_line_at_-1, by={C}}];
\draw (B) -- (C);

\coordinate (D') at ($(C) + (45:2)$);
\path[name path=extension_of_line_segment_CD] (C) -- (D');
\coordinate[name intersections={of=extension_of_line_segment_CD and x-axis, by={D}}];
\draw (C) -- (D);

\coordinate (E') at ($(D) + (135:2)$);
\path[name path=extension_of_line_segment_DE] (D) -- (E');
\coordinate[name intersections={of=extension_of_line_segment_DE and horizontal_line_at_1, by={E}}];
\draw (D) -- (E);

\coordinate (F) at ($(E) + (15:3)$);
\draw (E) -- (F);
\draw (A) -- (F);


%Points P and Q in the hexagon are plotted. Line segment $\overline{PQ}$ is not contained in the
%hexagon.
\coordinate (P) at (-4,0.75);
\draw[fill] (P) circle (1.5pt);
\coordinate (Q) at (-4,-0.75);
\draw[fill] (Q) circle (1.5pt);
\draw (P) -- (Q);
%Points P and Q are labeled.
\coordinate (label_for_P) at ($(P)!-3mm!-90:(Q)$);
\node at (label_for_P){$P$};
\coordinate (label_for_Q) at ($(Q)!-3mm!90:(P)$);
\node at (label_for_Q){$Q$};

%A title is typeset.
\node[align=center,font=\bfseries, yshift=2em] (title) at (current bounding box.north){A set that is \\ not convex};
\end{tikzpicture}


\begin{tikzpicture}

%A hexagon is drawn which is symmetric across the x-axis.
\coordinate (A) at (0,0);
\coordinate (O) at (-5,0);

\path[name path=x-axis] (A) -- (O);
\path[name path=horizontal_line_at_-1] (-5,-1) -- (0,-1);
\path[name path=horizontal_line_at_1] (-5,1) -- (0,1);

\coordinate (B) at ($(A) + (-135:2.5)$);
\draw (A) -- (B);

\coordinate (C') at ($(B) + (165:3)$);
\path[name path=extension_of_line_segment_BC] (B) -- (C');
\coordinate[name intersections={of=extension_of_line_segment_BC and horizontal_line_at_-1, by={C}}];
\draw (B) -- (C);

\coordinate (D') at ($(C) + (45:2)$);
\path[name path=extension_of_line_segment_CD] (C) -- (D');
\coordinate[name intersections={of=extension_of_line_segment_CD and x-axis, by={D}}];
\draw[dashed] (C) -- (D);

\coordinate (E') at ($(D) + (135:2)$);
\path[name path=extension_of_line_segment_DE] (D) -- (E');
\coordinate[name intersections={of=extension_of_line_segment_DE and horizontal_line_at_1, by={E}}];
\draw[dashed] (D) -- (E);

\draw (C) -- (E);

\coordinate (F) at ($(E) + (15:3)$);
\draw (E) -- (F);
\draw (A) -- (F);


%A title is typeset.
\node[font=\bfseries, yshift=2em] (title) at (current bounding box.north){A convex set};
\end{tikzpicture}

\end{document}
user74973
  • 4,071

1 Answers1

1

And one more solution (It take me little more time because I allowed myself first to simplify the picture code :-) , i was lost in original one :-( ):

\documentclass{amsart}
    \usepackage{amsmath}
    \usepackage{amsfonts}
    \usepackage{tikz}
\usetikzlibrary{calc,angles,positioning,intersections}

    \begin{document}
\begin{tikzpicture}[
    node distance= 3mm and 5mm,
                    ]
%%%% first shape
\coordinate (O)     at (-5,0);
\coordinate (A)     at (0,0);
\coordinate (B)     at ($(A)+(135:2.5)$);
\coordinate (C)     at ($(B)+(-165:3)$);
%
\coordinate (B')    at ($(A)+(-135:2.5)$);
\coordinate (C')    at ($(B')+(165:3)$);
%
\path[name path=x-axis] (O) -- (A);
\path[name path=c-b_line] (C') -- (B);
\coordinate[name intersections={of=c-b_line and x-axis,by={D}}];

\draw (A) -- (B) -- (C) -- (D) -- (C') -- (B') -- cycle;

\coordinate (P) at (-4,0.75);
\coordinate (Q) at (-4,-0.75);
\draw[fill=black]   (P) circle (1.5pt) node[right] {$P$}  -- 
                    (Q) circle (1.5pt) node[right] {$Q$};
%% Title of second shape.
\node[font=\bfseries,align=center,
      above=of B -| {$(C |- A)!0.5!(A)$}] (title)   {A set that is \\
                                                     not convex};
%% Title of first shape.
\node[font=\bfseries,align=center,
      above=of B -| {$(C |- A)!0.5!(A)$}] (title)   {A set that is \\
                                                     not convex};
%%%% secod shape
\begin{scope}[transform canvas={xshift=6cm}]
\coordinate (O)     at (-5,0);
\coordinate (A)     at (0,0);

\coordinate (B)     at ($(A)+(135:2.5)$);
\coordinate (C)     at ($(B)+(-165:3)$);
%
\coordinate (B')    at ($(A)+(-135:2.5)$);
\coordinate (C')    at ($(B')+(165:3)$);
%
\path[name path=x-axis] (O) -- (A);
\path[name path=c-b_line] (B) -- (C');
\coordinate[name intersections={of=c-b_line and x-axis,by={D}}];

\draw (A) -- (B) -- (C) -- (C') -- (B') -- cycle;
\draw[dashed] (C) -- (D) -- (C');
%% Title of second shape.
\node[font=\bfseries,
      above=of B -| {$(C |- A)!0.5!(A)$}] (title)  {A convex set};
\end{scope}
\end{tikzpicture}
    \end{document}

enter image description here

Edit: Now I figured out, how to move second shape with xshift ... I correct my MWE accordingly. The picture is the same as before.

Edit (2): Some explanation of MWE. The node distance from library possitionig determine common distance between nodes if you say: \node[above right=of node name]. In this case, the horizontal distance is not used.

The node for (sub)image titles can have the same parameters and can be set up on beginning of picture, something like:

\begin{tikzpicture}[
title/.style = {font=\bfseries, align=center}]

and then in code use

\node[title, above=of M |-C] {title};

if the M is midpoint between A and O. Midpoint M can be determined as shown in above, however it can be simply set up width

\coordinate[right=25mm of A] (M);

As can be seen, I significantly strip the MWE in question simply because most of them I din't understand and they seems to me to be surplus. In compose of it I follow to given pictures.

Zarko
  • 296,517
  • You should edit your code. You have the same command following "%% Title of second shape" as you have following "%% Title of first shape". – user74973 Jul 27 '15 at 22:48
  • You have above=of B -| {$(C |- A)!0.5!(A)$} as an option in the node command to place the first title. (Recall that C and E are the two vertices on the hexagon that are furthest to the left. A is the vertex on the hexagon furthest to the right.) I know that C |- A is the intersection of the vertical line through C and the horizontal line through A. So, {$(C |- A)!0.5!(A)$} is the "horizontal midpoint" of the first display. If I call this point M, what is B -| M and above=of B -| M? – user74973 Jul 27 '15 at 22:57
  • (I see that in your code B is the highest vertex on the hexagon.) I guess that B -| M is the intersection of a horizontal line through B and a vertical line through M. OK. If my comments are correct, I would expect the middle of the title "A set that is not convex" to be typeset much lower. I don't know what above of does, though. By the way, shouldn't the syntax be something like above of={B -| M}? – user74973 Jul 27 '15 at 23:10
  • You also have node distance= 3mm and 5mm as an option to the tikzpicture environment. How is this interpreted by TikZ? – user74973 Jul 27 '15 at 23:11
  • OK. I'll give you an opportunity to answer my questions. Thanks. – user74973 Jul 27 '15 at 23:11
  • Really? They are almost the same, but not the same. Difference: the first one has option align=center, but it will not hurt, if they will be the same except of title (which is different, of course) if I correctly understand your first comment. The midpoint M you also can simply determined manually ... it is 2.5 cm left from A and title is above M |- C. – Zarko Jul 27 '15 at 23:21
  • node distance˙is option ofpositioninglibrary. It means that the distance between nodes are3mmin vertical direction and5mm` in horizontal direction (if you don't locally overwrite with other distance. – Zarko Jul 27 '15 at 23:26
  • No, the width of the first figure is not 5. I say that \coordinate (O) at (-5,0); but points C and and E are not on the vertical line x=-5. So, I would like to use your code. – user74973 Jul 27 '15 at 23:51
  • Is B -| M the intersection of a horizontal line through B and a vertical line through M? If this is correct, the intersection would be at the same height at B. How did the title get shifted upwards? I would like to position the title without using node distance= 3mm and 5mm, I think. Can I use something like \node at ($(B -| M) +(0,0.3)$);? – user74973 Jul 27 '15 at 23:57
  • Please, read my edit carefully. I give the examples, how to use it. Actually, this predefined distance is used only in positioning of title nodes. Al other nodes is explicit defined. If you don't like this simple solution, than determine this node position on own way, something as you write in your comment. Try it, if it works for you. Probably will help you to read description of positioning library in TikZ manual. – Zarko Jul 28 '15 at 00:07
  • Which manual? I looked at http://www.bu.edu/math/files/2013/08/tikzpgfmanual.pdf and there was nothing about positioning. – user74973 Jul 28 '15 at 00:26
  • We need to stop with learning/teaching through comments ... TikZ & PGF manual for version 3.0.0 is part of TikZ package, See your LaTeX distribution, it is stored in doc\generic\pgf. And, if you liked the answer, pleas accept it. – Zarko Jul 28 '15 at 00:33
  • That is a ridiculous comment that you made, especially considering that you changed my code so much after posting your answer! You also made an erroneous comment, which was confusing. It is false that you can "simply determined (the center of the hexagon) manually ... it is 2.5 cm left from A and title is above M |- C." I had not seen that you can use a title option for a node using positioning. The reason for my post was to properly put a title on two figures. Your code involved commands that I had not seen. Yes, of course, I am going to ask for clarification! – user74973 Jul 28 '15 at 01:14