10

I have the following code in LaTeX that produces a circle in a square with arrow lines, but I want to replace the circle with an irregular shape like the molecule in the image. How can I go about it?

\begin{figure}[H]
\centering
\begin{tikzpicture}[thick]
  %x-axes
  \draw [red](4,2) --(7,2);
  %\draw (4,3) --(7,3);
  %\draw (4,4) --(7,4);
  \draw [red,very thick](4,5) --(7,5);
  %y-axes
  \draw [red,very thick](7,2) --(7,5);
  %\draw (6,2) --(6,5);
  %\draw (5,2) --(5,5);
  \draw [red,very thick](4,2) --(4,5);
  %centre
  \fill[blue,very thick] (5.5,3.5) circle(.1);
  %arrows on the right
  \draw [-latex][dashed] (5.5,3.5) --(8,5);
  \draw [-latex][dashed] (5.5,3.5) --(8,4);
  \draw [-latex][dashed] (5.5,3.5) --(8,3);
  \draw [-latex][dashed] (5.5,3.5) --(8,2);
   %arrows on the left
  \draw [-latex][dashed] (5.5,3.5) --(3,2);
  \draw [-latex][dashed] (5.5,3.5) --(3,3);
  \draw [-latex][dashed] (5.5,3.5) --(3,4);
  \draw [-latex][dashed] (5.5,3.5) --(3,5);
  %arrows at the bottom
  \draw [-latex][dashed] (5.5,3.5) --(4,1);
  \draw [-latex][dashed] (5.5,3.5) --(5,1);
  \draw [-latex][dashed] (5.5,3.5) --(6,1);
  \draw [-latex][dashed] (5.5,3.5) --(7,1);
  %arrows at the top
  \draw [-latex][dashed] (5.5,3.5) --(4,6);
  \draw [-latex][dashed] (5.5,3.5) --(5,6);
  \draw [-latex][dashed] (5.5,3.5) --(6,6);
  \draw [-latex][dashed] (5.5,3.5) --(7,6);
  %draw a circle
  \node(circle) [circle, inner sep=0.7cm, draw=red!120] at (5.5,3.5) {};
\end{tikzpicture}
\end{figure}

Enter image description here

1

2 Answers 2

10

This is what you can do with the hobby package:

\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{hobby}

\begin{document}
\begin{tikzpicture}[scale=2]
  \draw [red,very thick](4,2) --(7,2);
  \draw [red,very thick](4,5) --(7,5);
  \draw [red,very thick](7,2) --(7,5);
  \draw [red,very thick](4,2) --(4,5);

  \fill[blue,very thick] (5.5,3.5) circle(.1);
  %arrows on the right
  \draw [-latex][dashed] (5.5,3.5) --(8,5);
  \draw [-latex][dashed] (5.5,3.5) --(8,4);
  \draw [-latex][dashed] (5.5,3.5) --(8,3);
  \draw [-latex][dashed] (5.5,3.5) --(8,2);
   %arrows on the left
  \draw [-latex][dashed] (5.5,3.5) --(3,2);
  \draw [-latex][dashed] (5.5,3.5) --(3,3);
  \draw [-latex][dashed] (5.5,3.5) --(3,4);
  \draw [-latex][dashed] (5.5,3.5) --(3,5);
  %arrows at the bottom
  \draw [-latex][dashed] (5.5,3.5) --(4,1);
  \draw [-latex][dashed] (5.5,3.5) --(5,1);
  \draw [-latex][dashed] (5.5,3.5) --(6,1);
  \draw [-latex][dashed] (5.5,3.5) --(7,1);
  %arrows at the top
  \draw [-latex][dashed] (5.5,3.5) --(4,6);
  \draw [-latex][dashed] (5.5,3.5) --(5,6);
  \draw [-latex][dashed] (5.5,3.5) --(6,6);
  \draw [-latex][dashed] (5.5,3.5) --(7,6);
  %draw a circle
  \node(circle) [circle, inner sep=0.7cm, draw=red!120] at (5.5,3.5) {};

  \coordinate[label=below left:{$A$}] (A) at (4+0.8,2+0.8);
  \coordinate[label=left:{$B$}]       (B) at (4+0.4,2+1.5);
  \coordinate[label=above:{$C$}]      (C) at (4+0.5,2+2.5);
  \coordinate[label=above:{$D$}]      (D) at (4+1.0,2+2.0);
  \coordinate[label=below:{$E$}]      (E) at (4+1.8,2+2.7);
  \coordinate[label=below left:{$F$}] (F) at (4+2.4,2+2.4);
  \coordinate[label=left:{$G$}]       (G) at (4+2.7,2+1.8);
  \coordinate[label=right:{$H$}]      (H) at (4+2.0,2+1.0);
  \coordinate[label=right:{$I$}]      (I) at (4+2.5,2+0.5);
  \coordinate[label=below left:{$J$}] (J) at (4+1.5,2+0.4);

  \draw[blue,very thick] (A) to [closed, curve through = {(B) (C) (D) (E) (F) (G) (H) (I) (J)}] (A);

  \foreach \j in {A,...,J}{\fill[blue] (\j) circle (0.04);}

\end{tikzpicture}

\end{document}

enter image description here

2
  • 1
    Thanks a lot kabenyuk. This is what I needed. Thanks again. Mar 27, 2022 at 11:56
  • I am glad to have been of some help!
    – kabenyuk
    Mar 27, 2022 at 14:41
9

As your MWE had a strange sketch not so related with the result you wanted, I got a little bit confused, sorry. Anyhow, I will present:

  1. A redraw of your original code, as a base of how some commands work.
  2. A colorful draw of your desired result.
  3. Another method to draw a smooth curve, from which you can choose which one to use.

I let colors in order to make it easier to identify who does what, but not exactly the ones you presented. You may mix and edit code at your will.

MWE 01

Please, notice how much simpler are some the code when some commands are used. I invite you check A very minimal introduction to TikZ.

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[thick]
  \draw [red,very thick] (4,2) rectangle ++(4,4);
  \fill[blue,very thick] (6,4) coordinate (A) circle (0.2); 
  \foreach \t in {0,20, ..., 360}{
    \draw [-latex, dashed]  (A) -- ++(\t:3.5);
  };
  \node(circle) [circle, inner sep=0.7cm, draw=red!120] at (A) {};
\end{tikzpicture}
\end{document}

enter image description here

MWE 02

The main MWE. The closed smooth parth is drawn with ... plot[smooth cycle, tension=.7] coordinates {...};. Notice how scope compacts the code to applied every node, it makes the code much more clean and clear to read.

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[thick]
  \draw [magenta,very thick] (0,0) node[left]{Solvent} rectangle ++(6.8,5.5);
  
  \draw[green!50!black]  plot[smooth cycle, tension=.7]
  coordinates {(0.8,0.6) (3.3,1.2) (3.3,2.5) (4.4,2.9) (5.5,3.5) (4.8,5.1) (1.3,3.6)};
  
  \node at (7.2,0.1) {$\partial \Omega$};
  \node[green!50!black] at (5.6,5.1) {$\Gamma$};
  \node[green!50!black] at (2.9,5) {$\Omega^{+}$};
  \node[green!50!black] at (2.9,4.1) {$\Omega^{-}$};
  \node[green!50!black] at (2.9,3.1) {Molecule};
  \node[orange] (B) at (5.5,1) {Mobile Ions};
  
  % Base syntax
  % \node[circle, red, inner sep=0.1cm, draw] (circle) at (4.1,-1.3) {$+$};
  % \node[circle, blue, inner sep=0.1cm, draw] (circle) at (2.4,-2.9) {$-$};
  
  \begin{scope}[every node/.style={circle, red, inner sep=0.1cm, draw}]
    \node at (3.7,3.8) {$+$};
    \node at (2,2.2) {$+$};
    \node at (1.7,3.1) {$+$};
  \end{scope}
  
  \begin{scope}[every node/.style={circle, blue, inner sep=0.1cm, draw}]
    \node at (4.6,4.4) {$-$};
    \node at (1.3,1.4) {$-$};
    \node at (1.2,2.3) {$-$};
  \end{scope}
  
  % Base syntax
  % \node[red] (circle) at (6.6,-0.8) {$+$};
  % \node[blue] (circle) at (2,-0.3) {$-$};
  
  \begin{scope}[every node/.style={cyan}]
    \node (P) at (4.7,2.2) {$+$};
    \node at (0.2,1.8) {$+$};
    \node at (0.6,3.8) {$+$};
  \end{scope}
  
  \begin{scope}[every node/.style={teal}]
    \node (N) at (6.1,4.1) {$-$};
    \node at (1.6,4.6) {$-$};
    \node at (0.6,4.6) {$-$};
  \end{scope}
  
  \draw[-latex] (B) -- (N);
  \draw[-latex] (B) -- (P);
\end{tikzpicture}
\end{document}

enter image description here

MWE 03

The alternative to plot[smooth] is using Bezier curves with (A) .. controls (B) and (C) .., where (A) , (B) and (C) are given coordinates.

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
  \draw[purple] (9.8,2.5) .. controls (10.8,2) and (12.3,2) .. (11.3,3) .. controls (10.8,3.5) and (12.3,3) ..
  (12.8,4) .. controls (13.3,4.5) and (10,5.4) ..
  (8.8,4.5) .. controls (7.3,3.5) .. cycle;
\end{tikzpicture}
\end{document}

enter image description here

2
  • 1
    Thanks FHZ. I will try inserting this image in my code. Thanks Mar 27, 2022 at 9:22
  • @user3307971, glad to help. @kabenyuk answer is also a interesting one using to [closed, curve through = .... Once again, sorry not drawing the exactly path your have shown, but showing techniques to achieve that result.
    – FHZ
    Mar 27, 2022 at 22:54

You must log in to answer this question.

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