放弃这条路。你忘了一个分号吗?

放弃这条路。你忘了一个分号吗?

我尝试在 VScode 上绘制电荷和电场矢量,但总是弹出错误消息

\documentclass{report}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
% Define charges location and magnitude
\def\chargeA{(0,0)}
\def\chargeB{(4,0)}
\def\chargeASize{0.3}
\def\chargeBSize{0.3}

% draw charges
\draw (\chargeA) circle (\chargeASize) node[above] {$+q$} ;
                       
\draw (\chargeB) circle (\chargeBSize) node[above] {$-q$} ;

% draw electric field vector
\draw [->, thick] (\chargeA) -- node[above] {$\vec{E}$} (\chargeB) ;
                                

\draw [->, thick] (\chargeB) -- node[below] {$\vec{E}$} (\chargeA) ;
\end{tikzpicture}
\end{document}

像这样

Package tikz: Giving up on this path. Did you forget a semicolon?.

但我已在行尾加上了分号。

答案1

你的图表

在此处输入图片描述

可以用以下 MWE (最小工作示例) 绘制:

\documentclass[margin=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,
                positioning,
                quotes}

\begin{document}
    \begin{tikzpicture}[
node distance = 4cm,
            > = Straight Barb,
charge/.style = {circle, draw, fill=white, 
                 inner sep=0pt, minimum size = 3mm,
                 label=#1, node contents={}},
                        ]
% Define charges location 
\node (A) [charge=$+q$];
\node (B) [charge=$-q$, right=of A];
%
% field
\draw[<->]   (A) to ["$\vec{E}$", "$\vec{E}$" '] (B);% 
    \end{tikzpicture}
\end{document}

或者你可能喜欢以下风格的图表:

在此处输入图片描述

\documentclass[margin=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,
                positioning,
                quotes}

\begin{document}
    \begin{tikzpicture}[
node distance = 4cm,
            > = Straight Barb,
    bend angle= 15,
charge/.style = {circle, draw, fill=white, 
                 inner sep=0pt, minimum size = 3mm,
                 label=#1, node contents={}},
every edge/.append style = {draw, ->}
                        ]
% Define charges location 
\node (A) [charge=$+q$];
\node (B) [charge=$-q$, right=of A];
%
% field
\draw   (A) edge [bend left, "$\vec{E}$"] (B)
        (B) edge [bend left, "$\vec{E}$"] (A); 
    \end{tikzpicture}
\end{document}

相关内容