• Extra kerning between CJK characters but not latin letters with `text width` in TikZ?
    by Explorer on November 21, 2026 at 4:24 pm

    Let's see the following code: \documentclass[tikz,border=5pt]{standalone} \usepackage[fontset=fandol]{ctex} \begin{document} \tikz\node[draw,rectangle,text width=4cm,align=center]{你好世界}; \tikz\node[draw,rectangle,minimum width=4cm]{你好世界}; \tikz\node[draw,rectangle,text width=4cm,align=center]{XXX}; \tikz\node[draw,rectangle,minimum width=4cm]{XXX}; \end{document} No matter with xelatex nor lualatex, with TL2026, gives: What caused the extra spacing around the CJK characters 「你好世界」? Could the extra kerning be avoided?

  • Cannot move a node in the axis environment
    by kronenbouh on November 21, 2026 at 4:13 pm

    I draw a simple graph using tikz and the axis environment. I placed a node to indicate the name of the function, however the argument "pos" does not seem to have any impact on the actual position of the node. I cannot figure out why. Below is a minimal compilable code. \documentclass[tikz,border=3mm]{standalone} \usepackage{pgfplots} \begin{document} \begin{center} \begin{tikzpicture} \begin{axis}[ xmin = -0.2, xmax=11.2, ymin=-0.2, ymax=620, axis x line=middle, axis y line=middle, axis line style=->, grid=both, grid style = {opacity=.5}, xtick={0, 1, ..., 11}, ytick={0, 50, ..., 600}, clip=true, x=1cm, y=0.01cm, ] \addplot[no marks, black, -, thick] expression[domain=0:11, samples=101]{(-22*x^2+220*x+50}; node[pos=0.8, above right] {$f$}; \end{axis} \end{tikzpicture} \end{center} \end{document} Thank you for your help.

  • Using scope to do an affine transformation of a polygon
    by kronenbouh on November 20, 2026 at 1:23 pm

    I'm currently inserting a tikzpicture in my latex document, my code is below: \documentclass[tikz,border=3mm]{standalone} \begin{document} \begin{center} \begin{tikzpicture}[scale=0.6] \draw[xstep=1cm, ystep=1cm, gray!30] (0,0) grid (15,7); \coordinate (A) at (1,1); \coordinate (B) at (1,6); \coordinate (C) at (4,6); \coordinate (D) at (4,4); \coordinate (E) at (2,3); \coordinate (F) at (4,2); \coordinate (G) at (4,1); \foreach \P [remember=\P as \lastP (initially A)] in {A,...,G,A} { \draw[black] (\lastP) -- (\P); } % the part i'm struggling with \begin{scope}[cm={-1,0,0,1,(8,0)}] \foreach \P [remember=\P as \lastP (initially A)] in {A,...,G,A} { \draw[black] (\lastP) -- (\P); } \end{scope} \end{tikzpicture} \end{center} \end{document} I would like to draw the symmetric of the (A) to (G) polygon along the (CG) axis. My code produces the original polygon but not the transformed one. Could someone help me fix this issue?

  • How to Create a Chart of Taiwanese Bus Routes Using LaTeX?
    by mathrm alpha on November 20, 2026 at 4:58 am

    For example, I want to use LaTeX to create a route map for Taiwan's intercity buses: Here's the code below: \documentclass[border=0pt]{standalone} \usepackage{fontspec} \usepackage{etoolbox} \usepackage{amssymb} \usepackage{xeCJK} \usepackage{tikz} \usetikzlibrary{arrows.meta,calc,shapes.geometric} \setCJKmainfont{Noto Sans CJK TC} \setmainfont{DejaVu Sans} \definecolor{redbus}{HTML}{B60000} \definecolor{goldbus}{HTML}{A38743} \definecolor{purplebus}{HTML}{B61C73} \definecolor{bluebus}{HTML}{126090} \begin{document} \begin{tikzpicture}[x=0.25mm,y=-0.25mm,inner sep=0pt,outer sep=0pt] \path[use as bounding box] (0,0) rectangle (960,765);\clip (0,0) rectangle (960,765); \fill[white] (0,0) rectangle (960,765); \fill[redbus] (0,0) rectangle (960,34); \node[white,font=\fontsize{9}{11}\selectfont] at (479,18) {大南汽車:0800-089-456 土城站:2274-1827 三重客運:0800-090-607 土城站:2261-4734}; \node[anchor=west,font=\bfseries\fontsize{76}{80}\selectfont,text=redbus] at (12,124) {265}; \draw[redbus,line width=2pt] (260,46)--(260,200); \node[anchor=north west,align=left,font=\fontsize{7}{11}\selectfont] at (269,40) { 一、起迄點:土城-行政院(經明德路)/土城-行政院(經中央路)\\ 二、頭末班車時間:05:30--22:00\\ 三、收費方式:二段票收費\\ 四、分段點/緩衝區:往程:埔墘(三民路)-捷運龍山寺站/返程:龍山寺(西園)-埔墘(三民路)\\ 五、發車間隔:標示紅色時刻為例假日行駛參考\\ \hspace{12pt}經明德路:05:30/06:20/07:00/07:10/07:40/08:40/09:30/10:20/11:10/11:25/11:50\\ \hspace{96pt}12:00/12:50/13:10/13:40/13:50/14:30/14:50/15:35/16:20/16:40/17:20\\ \hspace{96pt}18:15/19:05/19:55/20:45/21:35/22:00\\ \hspace{12pt}經中央路:06:00/06:35/06:50/07:20/07:35/08:15/09:00/09:15/09:50/10:05/10:45/11:40/12:20/12:35\\ \hspace{96pt}13:15/13:30/14:15/14:45/15:15/16:00/16:15/16:50/17:05/17:45/18:40/19:30/20:20/21:10 }; \draw[purplebus,line width=1.6pt,rounded corners=6pt,fill=white] (803,45) rectangle (948,124); \draw[purplebus,line width=1pt] (803,82)--(948,82); \node[align=center,font=\fontsize{8}{10}\selectfont,text=purplebus] at (875,63) {全程車假日\\狗狗公車固定班次}; \node[align=center,font=\fontsize{8}{10}\selectfont] at (875,103) {發車班次(經明德路)\\06:20/09:30/14:30}; \draw[redbus!40,line width=1.6pt] (23,216)--(937,216); % Routes: top row eastward, right descent, lower row westward, left return \draw[goldbus,line width=3pt,rounded corners=15pt] (34,263)--(901,263)--(924,263)--(924,467)--(52,467)--(35,467)--(35,414)--(127,414)--(127,466); \draw[goldbus,line width=3pt,rounded corners=9pt] (238,263)--(238,294)--(102,294); \draw[goldbus,line width=3pt,rounded corners=11pt] (509,263)--(509,357)--(570,357)--(570,263); \draw[goldbus,line width=3pt,rounded corners=10pt] (743,263)--(743,356)--(815,356)--(815,263); \draw[white,line width=5pt] (245,263)--(485,263); \draw[black,line width=1.7pt] (245,263)--(485,263); \node[font=\bfseries\fontsize{17}{18}\selectfont,rotate=0] at (522,287) {\textcolor{goldbus}{\Large$\triangleleft$}}; \node[font=\bfseries\fontsize{17}{18}\selectfont] at (738,287) {\textcolor{goldbus}{\Large$\triangleleft$}}; % pale separator \draw[redbus!30,line width=2pt] (25,660)--(935,660); % Editable stop marker and vertically set station name % #1=x coordinate, #2=y coordinate, #3=ordinary/red, #4=top/bottom, #5=stacked name \newcommand{\Stop}[5]{% \ifstrequal{#3}{red}{ \draw[redbus,line width=1.45pt,fill=white] (#1,#2) circle (7); \fill[redbus] (#1,#2) circle (2.5); }{\draw[black,line width=1.35pt,fill=white] (#1,#2) circle (7);} \ifstrequal{#4}{top}{ \node[anchor=north,align=center,font=\fontsize{9}{10}\selectfont] at (#1,{#2+14}) {#5}; }{\node[anchor=north,align=center,font=\fontsize{9}{10}\selectfont] at (#1,{#2+14}) {#5};} } \Stop{33}{263}{red}{top}{土\\城\\站} \Stop{63}{263}{ordinary}{top}{大\\同\\莊\\園} \Stop{101}{294}{red}{top}{土\\城\\站} \Stop{130}{294}{ordinary}{top}{看\\守\\所} \Stop{159}{294}{ordinary}{top}{捷\\運\\海\\山\\站} \Stop{189}{294}{ordinary}{top}{明\\德\\路\\一\\段} \Stop{218}{294}{ordinary}{top}{明\\德\\路} \Stop{255}{263}{ordinary}{top}{日\\新\\里} \Stop{284}{263}{ordinary}{top}{貨\\饒\\里} \Stop{313}{263}{ordinary}{top}{中\\央\\路} \Stop{342}{263}{red}{top}{中\\央\\一\\站} \Stop{370}{263}{ordinary}{top}{南\\雅\\站} \Stop{399}{263}{ordinary}{top}{亞\\東\\技\\術\\學\\院} \Stop{428}{263}{ordinary}{top}{信\\義\\路} \Stop{456}{263}{ordinary}{top}{郵\\局} \Stop{484}{263}{red}{top}{壽\\德\\新\\村} \Stop{513}{263}{ordinary}{top}{重\\慶\\國\\中} \Stop{543}{263}{red}{top}{重\\慶\\國\\中} \Stop{570}{263}{ordinary}{top}{重\\慶\\國\\中} \Stop{596}{263}{ordinary}{top}{重\\慶\\忠\\孝\\路\\口} \Stop{625}{263}{ordinary}{top}{忠\\孝\\路} \Stop{655}{263}{ordinary}{top}{中\\興\\醫\\院} \Stop{684}{263}{ordinary}{top}{福\\星\\里} \Stop{715}{263}{ordinary}{top}{鄉\\雲\\里} \Stop{757}{263}{ordinary}{top}{後\\站\\商\\圈} \Stop{800}{263}{ordinary}{top}{後\\站\\商\\圈} \Stop{842}{263}{red}{top}{介\\壽\\公\\園} \Stop{872}{263}{ordinary}{top}{板\\橋\\外\\站} \Stop{901}{263}{ordinary}{top}{民\\權\\路\\口} \Stop{52}{467}{red}{bottom}{立\\法\\院} \Stop{80}{467}{red}{bottom}{成\\功\\中\\學} \Stop{110}{467}{red}{bottom}{捷\\運\\善\\導\\寺\\站} \Stop{139}{467}{ordinary}{bottom}{臺\\北\\車\\站} \Stop{168}{467}{ordinary}{bottom}{中\\華\\路\\北\\站} \Stop{197}{467}{red}{bottom}{捷\\運\\西\\門\\站} \Stop{226}{467}{ordinary}{bottom}{小\\南\\門} \Stop{255}{467}{red}{bottom}{桂\\林\\昆\\明\\街\\口} \Stop{285}{467}{red}{bottom}{桂\\林\\路} \Stop{314}{467}{ordinary}{bottom}{龍\\山\\寺} \Stop{345}{467}{ordinary}{bottom}{龍\\山\\國\\寺} \Stop{379}{467}{red}{bottom}{捷\\運\\龍\\山\\寺\\站} \Stop{410}{467}{ordinary}{bottom}{大\\理\\服\\飾} \Stop{440}{467}{ordinary}{bottom}{西\\園\\2\\9\\服\\飾\\基\\地} \Stop{470}{467}{ordinary}{bottom}{華\\江\\高\\中} \Stop{507}{467}{ordinary}{bottom}{西\\園\\路\\二\\段} \Stop{537}{467}{ordinary}{bottom}{光\\復\\橋} \Stop{574}{467}{ordinary}{bottom}{光\\復\\橋} \Stop{603}{467}{ordinary}{bottom}{埔\\墘\\派\\出\\所} \Stop{633}{467}{ordinary}{bottom}{埔\\墘} \Stop{662}{467}{ordinary}{bottom}{埔\\墘\\里} \Stop{691}{467}{ordinary}{bottom}{自\\來\\水\\公\\司} \Stop{720}{467}{ordinary}{bottom}{正\\隆\\廣\\場} \Stop{750}{467}{ordinary}{bottom}{中\\山\\路\\口} \Stop{779}{467}{red}{bottom}{捷\\運\\板\\新\\站} \Stop{808}{467}{red}{bottom}{中\\山\\漢\\生\\路\\口} \Stop{838}{467}{ordinary}{bottom}{新\\北\\市\\政\\府} \Stop{868}{467}{ordinary}{bottom}{新\\北\\板\\橋\\公\\車\\站} \Stop{898}{467}{ordinary}{bottom}{新\\北\\市\\政\\府} % numbered major connection stations \node[font=\bfseries\fontsize{16}{16}\selectfont,text=redbus] at (101,244) {1}; \node[font=\bfseries\fontsize{16}{16}\selectfont,text=redbus] at (159,244) {2}; \node[font=\bfseries\fontsize{16}{16}\selectfont,text=redbus] at (379,441) {3}; \node[font=\bfseries\fontsize{16}{16}\selectfont,text=redbus] at (197,441) {4}; \node[font=\bfseries\fontsize{16}{16}\selectfont,text=redbus] at (139,441) {5}; \node[font=\bfseries\fontsize{16}{16}\selectfont,text=redbus] at (80,441) {6}; % small transfer station pictograms \foreach \x/\y in {160/402,139/590,197/577,379/600,779/584,110/596}{ \fill[bluebus] (\x-7,\y-8) rectangle (\x+7,\y+8); \node[white,font=\bfseries\fontsize{12}{12}\selectfont] at (\x,\y) {\textsf{M}}; } % lower panel, note legends and English data \node[anchor=north west,align=left,font=\fontsize{8}{11}\selectfont] at (18,667) {圖例說明:\\\textcolor{redbus}{\large$\odot$} 起迄點 \textcolor{bluebus}{\bfseries M} 捷運站\\$\circ$ 雙邊設站 \textcolor{redbus}{$\triangleright$} 單邊設站\\$\blacklozenge$ 分段點/緩衝區}; \draw[redbus,line width=1.1pt] (201,667)--(201,757); \node[anchor=north west,align=left,font=\fontsize{7}{9}\selectfont] at (205,667) {\textbullet\ Tucheng-Executive Yuan\\\textbullet\ Hours of Operation : 05:30--22:00\\\textbullet\ Fare Zones : 2X\\\textbullet\ Fare Increase/Buffer Zone :\\Depart : Puqian(Sanmin Rd.)--MRT Longshan Temple Sta.\\Return : Longshan Temple(Xiyuan)--Puqian(Sanmin Rd.)\\\textbullet\ Headway : Fixed Time}; \draw[redbus,line width=1.1pt] (597,667)--(597,756); \node[anchor=north west,align=left,font=\fontsize{8}{10}\selectfont,text=goldbus] at (604,667) {1.Tucheng Stop\\2.MRT Haishan Sta.\\3.MRT Longshan Temple Sta.\\4.MRT Ximen Sta.\\5.Taipei Main Sta. (Zhongxiao)\\6.MRT Shandao Temple Sta.}; % accessible service mark \draw[bluebus,line width=2pt,rounded corners=7pt] (884,671) rectangle (940,728); \node[font=\fontsize{43}{46}\selectfont,text=bluebus] at (912,699) {♿}; \node[font=\fontsize{8}{10}\selectfont,text=bluebus,align=center] at (911,746) {行駛車輛有\\低地板公車}; \node[anchor=south west,font=\fontsize{7}{8}\selectfont] at (16,761) {108.12.31}; \end{tikzpicture} \end{document} In particular, I noticed that the arrow and the text layout didn't quite line up.

  • TikZ: Tangent to the x-axis with calc library only
    by cis on November 19, 2026 at 9:33 pm

    I want to produce something like this: It is no problem to draw that tangent as a plot or an analytic function, but I wonder: Is there a solution using only the calc library to add the tangent (from the point P on the circle to the x-axis)? Here is my (incomplete) MWE: \documentclass[margin=5pt, tikz]{standalone} \usepackage{tikz} \usetikzlibrary{calc} \pgfmathsetmacro\r{3} \pgfmathsetmacro\w{77} \begin{document} \begin{tikzpicture} \coordinate[] (O) at (0,0); \coordinate[label=$P$] (P) at (\w:\r); \draw[help lines, step=0.5] (-5,-5) grid (15,5); %% CoSy \draw[->] (-5,0) -- (15,0) node[below] {$x$}; \draw[->] (0,-5) -- (0,5) node[left] {$y$}; %% Circle \draw[blue, thick] (0,0) circle[radius=\r]; \draw[] (0,0) -- (P); %% Tangent (part) \draw[red, thick] ($(P)!6cm!90:(O)$) -- ($(P)!2cm!-90:(O)$); %% Points \foreach \P in {P} \draw[fill=black!1] (\P) circle[radius=1.5pt]; \end{tikzpicture} \end{document}

  • How to make the `\ddots` centering of the bounding box?
    by Explorer on October 17, 2026 at 4:37 pm

    This post is similar to this one, but not exactly the same... With the following code: \documentclass{article} \usepackage{amsmath} \usepackage{tikz} \usetikzlibrary{matrix} \setlength{\fboxsep}{0pt} \begin{document} \[ \begin{tikzpicture} \matrix (magic) [ matrix of math nodes, nodes={circle,draw=magenta,minimum size=1cm}, left delimiter=(, right delimiter=) ] { 1 \\ & 1 \\ & & \ddots \\ & & & \fbox{$\ddots$} \\ & & & & 1 \\ }; \draw[teal,dashed] (magic-1-1.center) -- (magic-5-5.center); \end{tikzpicture} \] \end{document} What I want was the following: only typeset three dots, thus \Ddots with nicematrix was not so flexible; I want the "slope" of \ddots was parallel with the teal,dashed line; I want to ellnimate the extra height of the bounding box of \ddots(why?It looks not good...) It recalled the classical \rvdots here, and we know: $ latexdef \ddots \ddots: macro:->\protect \ddots \ddots : macro:->\mathinner {\mkern 1mu\raise 7\p@ \vbox {\kern 7\p@ \hbox {.}}\mkern 2mu \raise 4\p@ \hbox {.}\mkern 2mu\raise \p@ \hbox {.}\mkern 1mu} I guessed slightly modify the commands could help? Any suggestions? \makeatletter % \DeclareRobustCommand{\rvdots}{% % \vbox{ % \baselineskip4\p@\lineskiplimit\z@ % \kern-\p@ % \hbox{.}\hbox{.}\hbox{.} % }} \DeclareRobustCommand{\rddots}{% \mathinner {% \mkern 1mu\raise 7\p@% \vbox{\kern 1\p@ \hbox {.}}% \mkern 0.4mu \raise 4\p@ \hbox {.}% \mkern 0.4mu\raise \p@ \hbox {.}% \mkern 1mu% }% } \makeatother Edit1 If I zoomed in with with cfr's answer: \makeatletter \newcommand\exddots {% \mathinner {% \mkern 1mu\raise 3.5\p@ % 7 -> 3.5 \vbox {\kern 0\p@ \hbox {.}}% 7 -> 0 \mkern 2mu \raise -0.5\p@ \hbox{.}% 4 -> -0.5 \mkern 2mu \raise -4\p@ \hbox{.}% \p@ -> -4 \mkern 1mu }% } \makeatother Edit2 Just to show the example of cfr's comment: Is the content or, at least, the size of the content in the matrix fixed? because the angle of the diagonal obviously will vary if that varies. so unless you want this only as a one-off, you maybe need to think about how this should work. should \exddots really look different each time and different from any use in maths mode? or do you want to fix the matrix dimensions to keep the angle constant? or ...? I don't think it will look so good if the dots look different in different cases. at least, that is not how it usually would work in latex? \documentclass{article} \usepackage{amsmath} \usepackage{nicematrix} \usepackage{tikz} % \usepackage{kpfonts-otf} \begin{document} \[ \begin{pNiceMatrix} 3 & & & \\ & 555 & & \\ & & \ddots & \\ & & & 66666 \CodeAfter \tikz{ \draw[teal, ultra thin] (1-1.center) -- (4-4.center); \draw[magenta, ultra thin] (2-2.south east) -- (4-4.north west); } \end{pNiceMatrix}\qquad \begin{pNiceMatrix} 3 & & & \\ & 555 & & \\ & & \Ddots & \\ & & & 66666 \CodeAfter \tikz{ \draw[teal, ultra thin] (1-1.center) -- (4-4.center); \draw[magenta, ultra thin] (2-2.south east) -- (4-4.north west); } \end{pNiceMatrix} \] \[ \begin{pNiceMatrix} a & & & \\ & b & & \\ & & \ddots & \\ & & & d \CodeAfter \tikz{ \draw[teal, ultra thin] (1-1.center) -- (4-4.center); \draw[magenta, ultra thin] (2-2.south east) -- (4-4.north west); } \end{pNiceMatrix}\qquad \begin{pNiceMatrix} a & & & \\ & b & & \\ & & \Ddots & \\ & & & d \CodeAfter \tikz{ \draw[teal, ultra thin] (1-1.center) -- (4-4.center); \draw[magenta, ultra thin] (2-2.south east) -- (4-4.north west); } \end{pNiceMatrix} \] \[ \begin{pNiceMatrix} x & & & & \\ & x+y & & & \\ & & \ddots & & \\ & & & \sqrt{x-y} & \\ & & & & \dfrac{x}{y} \CodeAfter \tikz{ \draw[teal, ultra thin] (1-1.center) -- (4-4.center); \draw[magenta, ultra thin] (2-2.south east) -- (4-4.north west); } \end{pNiceMatrix}\qquad \begin{pNiceMatrix} x & & & & \\ & x+y & & & \\ & & \Ddots & & \\ & & & \sqrt{x-y} & \\ & & & & \dfrac{x}{y} \CodeAfter \tikz{ \draw[teal, ultra thin] (1-1.center) -- (4-4.center); \draw[magenta, ultra thin] (2-2.south east) -- (4-4.north west); } \end{pNiceMatrix} \] \end{document} With latin-modern-math: With kpfonts-otf:

  • Help sketching the polarity of HCl, H₂O, and CO₂ in LaTeX
    by Abdelhalim AANIBA on May 24, 2026 at 3:25 pm

    I’m trying to create professional-looking molecular polarity diagrams in LaTeX (using TikZ or chemfig if possible). I need help sketching the partial charges cloud distribution for the following molecules: HCl H₂O CO₂ Moved from a non-answer (it's no solution, just problem description), and adding missing lines to make the posted code compile by MS-SPO this is so far my code, but I don't know how to connect all the clouds to give me a similar figure like the image \documentclass{article} \usepackage{tikz} \usetikzlibrary{calc} \begin{document} % Source - https://tex.stackexchange.com/a/763072 % Posted by Abdelhalim AANIBA, modified by community. See post 'Timeline' for change history % Retrieved 2026-05-25, License - CC BY-SA 4.0 \begin{tikzpicture}[scale=1.5] % Couleurs pour les nuages de charges \definecolor{negColor}{RGB}{255,100,100} % Rouge pour δ- \definecolor{posColor}{RGB}{100,100,255} % Bleu pour δ+ % Atomes \node[draw, circle, minimum size=0.6cm, fill=gray!30] (O) at (0,0) {O}; \node[draw, circle, minimum size=0.5cm, fill=gray!20] (H1) at (-0.8,-0.6) {H}; \node[draw, circle, minimum size=0.5cm, fill=gray!20] (H2) at (0.8,-0.6) {H}; % Liaisons \draw[thick] (O) -- (H1); \draw[thick] (O) -- (H2); % ===== NUAGE DE CHARGE NÉGATIVE (δ-) autour de l'oxygène ===== % Utilisation d'un shading radial pour un dégradé continu du centre vers l'extérieur \shade[inner color=negColor, outer color=negColor!10, opacity=0.3] (O) circle (0.8cm); % ===== NUAGES DE CHARGE POSITIVE (δ+) autour des hydrogènes ===== \shade[inner color=posColor, outer color=posColor!10, opacity=0.3] (H1) circle (0.5cm); \shade[inner color=posColor, outer color=posColor!10, opacity=0.3] (H2) circle (0.5cm); % Petits symboles δ- et δ+ (optionnels, très discrets) \node[red] at ($(O)+(0.08,1)$) {$2\delta^-$}; \node[blue] at ($(H1)+(0.05,-0.7)$) {$\delta^+$}; \node[blue] at ($(H2)+(0.05,-0.7)$) {$\delta^+$}; \end{tikzpicture} \end{document}

  • pgfmath function with loop not supported by addplot
    by Yijun Yuan on February 22, 2025 at 8:35 am

    The following code \documentclass{article} \usepackage{pgfplots} \usetikzlibrary{math} \begin{document} \begin{tikzpicture} \tikzmath{ function test(\x,\y){ \res=0; for \z in {1,...,3} { \res=\res+\x+\y; }; return \res; }; } \begin{axis} \addplot3[surf,variable=\x,variable y=\y]({\x},{\y},{test(\x,\y)}); \end{axis} \end{tikzpicture} \end{document} will give error Illegal unit of measure (pt inserted). <to be read again> Y l.21 ...=\x,variable y=\y]({\x},{\y},{test(\x,\y)}); while the code \documentclass{article} \usepackage{pgfplots} \usetikzlibrary{math} \begin{document} \tikzmath{ function test(\x,\y){ \res=0; for \z in {1,...,3} { \res=\res+\x+\y; }; return \res; }; \res = test(1,2); print{\res}; } \end{document} works as expected (prints 9.0). Why is that? (This function is just a MWE, so rather than replacing test(\x,\y) with (\x+\y)*3, I'd like to know the reason behind this error)

  • Drawing rods at various positions and angles along a parabola
    by Ismael Joaquim on February 6, 2025 at 10:40 am

    \documentclass{standalone} \usepackage{tikz} \begin{document} \begin{tikzpicture} % Define the parabola \coordinate (A) at (0,4); % Vertex of the parabola \coordinate (B) at (-3,0); % Left endpoint \coordinate (C) at (3,0); % Right endpoint \draw[thick] (A) parabola (B); % Draw left half of the parabola \draw[thick] (A) parabola (C); % Draw right half of the parabola % Draw a dashed grid for reference \draw[dashed, step=0.5] (-3,0) grid (3,4); % Define the length of the rod \def\rodLength{1.5} % Length of the rod % Draw the rod at various positions along the parabola \foreach \x in {-2.5, -1.5, -0.5, 0.5, 1.5, 2.5} { % Calculate the y position on the parabola using the equation y = 4 - (x^2)/2.25 \pgfmathsetmacro{\y}{4 - (\x * \x) / 2.25} % Calculate the slope of the parabola at this point (dy/dx = -x/1.125) \pgfmathsetmacro{\slope}{-\x / 1.125} % Calculate the rotation angle for the rod (in degrees) \pgfmathsetmacro{\angle}{atan(\slope)} % Draw the rod (thick blue line) rotated at the calculated angle \draw[line width=2mm, color=blue, rotate around={\angle:(\x,\y)}] (\x - \rodLength/2, \y) -- (\x + \rodLength/2, \y); % Mark the center of mass (red dot) \filldraw[red] (\x, \y) circle (2pt); } \end{tikzpicture} \end{document} Current result:

  • Why does \node work inside an axis environment while \pgfpointanchor fails?
    by cjorssen on November 14, 2024 at 10:47 pm

    Consider the following code, where a node named A is defined inside an axis environment at the end of an \addplot macro. \documentclass{standalone} \usepackage{pgfplots} \begin{document} \begin{tikzpicture} \begin{axis} \addplot coordinates {(0, 0) (1, 1)} node[pos = .5, sloped, above] (A) {to}; % Does not work here % \pgfmathanglebetweenpoints{\pgfpointanchor{A}{south west}}{\pgfpointanchor{A}{south east}} \node[rotate = 0, red] at (A) {YO}; \end{axis} % Works here % \pgfmathanglebetweenpoints{\pgfpointanchor{A}{south west}}{\pgfpointanchor{A}{south east}} \end{tikzpicture} \end{document} The low-level pgf command \pgfpointanchor does work outside of axis environment but does not work inside (the error I get is No shape named A is known). If I understand correctly, this is not surprising since pgfplots needs to process all the plots to compute the dimensions of the axis and then determine the coordinates of the node. What I don't understand is why the \node at (A) command works inside the axis environment. Could someone explain this behavior?

  • How do I make a 3D crystal lattice using TikZ?
    by Benzv2001 on April 22, 2024 at 8:03 am

    I've been trying to make a decent 3D crystal lattice using LaTeX and TikZ for a while now, and I finally managed to find some code where someone had made a perfect one. However, no matter what I try, instead of a 3D lattice, I get a mess of randomly oriented spherical parts. Below is the code that I used(https://github.com/dog-blood/utterly-unorganized-latex-code/tree/main/Chem/Unit%203%20Gasses): \begin{tikzpicture}[isometric view,rotate around z=180,line cap=round,line join=round,scale=0.8] \tikzset{xy/.style={canvas is xy plane at z=\a,shift={(0.5*\a,0.5*\a)},rotate=90*\i,fill=red1,draw=pag!30}, xz/.style={canvas is xz plane at y=\a,shift={(0.5*\a,0.5*\a)},rotate=90*\i,fill=red1,draw=pag!30}, yz/.style={canvas is yz plane at x=\a,shift={(0.5*\a,0.5*\a)},rotate=90*\i,fill=red1,draw=pag!30}, my ball/.style={top color=red2, bottom color=red2}} \def\a{3} % edge length \pgfmathsetmacro\r{0.25*sqrt(2.5)*\a} % sphere radius % background \foreach\i in {0,120,240} \draw[rotate=\i,color=pag!,my ball] (\a,0,0) --++ (0,\r,0) arc (90:135:\r) arc (0:60:\r cm) {[canvas is xz plane at y=0] arc (135:90:\r)} -- cycle; % sphere (center) \draw[my ball, color=pag] (0.5*\a,0.5*\a,0.5*\a) circle (\r cm); % foreground \foreach\i in {0,120,240} \draw[rotate=\i,my ball,color=pag] (\a,\a,0) --++ (60:\r cm) arc (60:120:\r cm) -- cycle; \foreach\s in {xy,xz,yz} \foreach\i in {1,...,4} \draw[\s] (45:{\a*sin(45)}) --++ (180:\r) arc (180:270:\r) -- cycle; \end{tikzpicture}

  • Draw Realistic 3D crystal structure of Diamond using Tikz
    by Akhil Akkapelli on August 24, 2022 at 1:32 pm

    Can I draw this same exact picture(from Draw realistic 3D crystal structures (diamond)) using TikZ in LaTeX? This is what I got till now. from the code: \documentclass[tikz,border=5pt]{standalone} \usepackage{tikz-3dplot} \begin{document} \begin{tikzpicture} \tdplotsetmaincoords{0}{60}{140} \pgfmathsetmacro{\d}{2.5} \draw[] (0,0,0) -- (4*\d,0,0) -- (4*\d,4*\d,0) -- (0,4*\d,0) -- (0,0,0); \draw[] (0,0,4*\d) -- (4*\d,0,4*\d) -- (4*\d,4*\d,4*\d) -- (0,4*\d,4*\d) -- (0,0,4*\d); \draw[] (0,0,4*\d) -- (4*\d,0,4*\d) -- (4*\d,0,0) -- (0,0,0) -- (0,0,4*\d); \draw[] (0,4*\d,4*\d) -- (4*\d,4*\d,4*\d) -- (4*\d,4*\d,0) -- (0,4*\d,0) -- (0,4*\d,4*\d); \coordinate (Aa) at (0,0,0); \coordinate (Ab) at (4*\d,0,0); \coordinate (Ac) at (0,4*\d,0); \coordinate (Ad) at (4*\d,4*\d,0); \coordinate (Ae) at (2*\d,2*\d,0); \coordinate (Ba) at (\d,\d,\d); \coordinate (Bb) at (3*\d,3*\d,\d); \coordinate (Ca) at (2*\d,0,2*\d); \coordinate (Cb) at (0,2*\d,2*\d); \coordinate (Cc) at (4*\d,2*\d,2*\d); \coordinate (Cd) at (2*\d,4*\d,2*\d); \coordinate (Da) at (3*\d,\d,3*\d); \coordinate (Db) at (\d,3*\d,3*\d); \coordinate (Ea) at (0,0,4*\d); \coordinate (Eb) at (4*\d,0,4*\d); \coordinate (Ec) at (0,4*\d,4*\d); \coordinate (Ed) at (4*\d,4*\d,4*\d); \coordinate (Ee) at (2*\d,2*\d,4*\d); \begin{scope}[line width=10,shade,shading angle=30] \draw[] (Ba) -- (Aa); \draw[] (Ba) -- (Ae); \draw[] (Bb) -- (Ae); \draw[] (Bb) -- (Ad); \draw[] (Ba) -- (Ca); \draw[] (Bb) -- (Cc); \draw[] (Bb) -- (Cd); \draw[] (Da) -- (Ca); \draw[] (Da) -- (Cc); \draw[] (Db) -- (Cb); \draw[] (Db) -- (Cd); \draw[] (Da) -- (Eb); \draw[] (Da) -- (Ee); \draw[] (Db) -- (Ee); \draw[] (Db) -- (Ec); \end{scope} \node[draw,circle,minimum size=1cm,shading=ball] at (Aa) {}; \node[draw,circle,minimum size=1cm,shading=ball] at (Ab) {}; \node[draw,circle,minimum size=1cm,shading=ball] at (Ac) {}; \node[draw,circle,minimum size=1cm,shading=ball] at (Ad) {}; \node[draw,circle,minimum size=1cm,shading=ball] at (Ae) {}; \node[draw,circle,minimum size=1cm,shading=ball] at (Ba) {}; \node[draw,circle,minimum size=1cm,shading=ball] at (Bb) {}; \node[draw,circle,minimum size=1cm,shading=ball] at (Ca) {}; \node[draw,circle,minimum size=1cm,shading=ball] at (Cb) {}; \node[draw,circle,minimum size=1cm,shading=ball] at (Cc) {}; \node[draw,circle,minimum size=1cm,shading=ball] at (Cd) {}; \node[draw,circle,minimum size=1cm,shading=ball] at (Da) {}; \node[draw,circle,minimum size=1cm,shading=ball] at (Db) {}; \node[draw,circle,minimum size=1cm,shading=ball] at (Ea) {}; \node[draw,circle,minimum size=1cm,shading=ball] at (Eb) {}; \node[draw,circle,minimum size=1cm,shading=ball] at (Ec) {}; \node[draw,circle,minimum size=1cm,shading=ball] at (Ed) {}; \node[draw,circle,minimum size=1cm,shading=ball] at (Ee) {}; \end{tikzpicture} \end{document}

  • Subheadings for Pie charts
    by Waseem on June 15, 2021 at 8:03 am

    Can anyone help me for writing subheading on top each Pi chart with the following code. Please see the example picture \documentclass[border=5mm]{standalone} \usepackage{pgf-pie} % version: https://github.com/pgf-tikz/pgf-pie Dec 26, 2020 \usepackage{etoolbox} \makeatletter \pretocmd{\pgfpie@slice}{% define label text with percentages for small values \pgfmathparse{#3 > 5}% \ifnum\pgfmathresult=1 % \def\txtlabel{#4}% original label for large values \else% \def\txtlabel{#4 (#3\%)}% label with percentage for small values \fi% }{\message{define label patch ok}}{\message{define label patch failed}} \patchcmd{\pgfpie@slice}% use label text instead of original argument #4 {\pgfpie@text={\pgfpie@midangle:#4}}% {\pgfpie@text={\pgfpie@midangle:\txtlabel}}% {\message{add pct patch ok}}% {\message{add pct patch failed}}% \def\pgfpie@numbertext#1{% don't print percentage in slice for small values \pgfpie@ifhidenumber{}{% \pgfmathparse{#1 > 5}% \ifnum\pgfmathresult=1 % \pgfpie@beforenumber#1\pgfpie@afternumber% \fi }% } \makeatother \begin{document} \begin{subfigure} \footnotesize \begin{tikzpicture} %\begin{scope}[scale=0.7] \pie[before number =, after number = {\%},text=pin, ]{ 18.9/1-20 Employees, 29.2/21-100 Employees, 22.6/101-500 Employees, 7.5/501-1000 Employees, 21.7/Over 1000 Employees } %\end{scope} \end{tikzpicture} \end{subfigure} %\begin{tikzpicture} %\pie[before number =, after number = {\%},text=pin, ]{ %42.3/ Combination of DDD and business capability, %29.8/ Only business capability, %27.9/ only DDD %} %\end{tikzpicture} \begin{subfigure} \footnotesize \begin{tikzpicture} %\begin{scope}[scale=0.7] \pie[before number =, after number = {\%},text=pin, ]{ 5.7/1 Practitioners, 43.4/2-3 Practitioners, 27.4/4-5 Practitioners, 9.4/6-7 Practitioners, 6/8-10 Practitioners, 8.5/Over 10 Practitioners } %\end{scope} \end{tikzpicture} \end{subfigure} \end{document}

  • Svg to Tikz doesn't work
    by Mohamed Hasnaoui on June 6, 2020 at 1:41 pm

    I'm using inkscape to draw a simple picture and i saved with svg extension I want to convert this svg file to Tikz code for latex I download the extensions from github https://github.com/paaguti/svg2tikz and i Extract the files tikz_export.py, tikz_export_effect.inx, and tikz_export_output.inx from svg2tikz-master/svg2tikz/extensions and put them in the share/extension directory inside the Inkscape installation directory. when i chose extension->exporter->export to tikz path i get this message and nothing happen; Inscape a reçu des données additionnelles du script exécuté. Le script n’a pas retourné d’erreur, mais ceci peut indiquer que les résultats ne sont pas ceux attendus. tikz_export.py:645: DeprecationWarning: Effect.OptionParser or optparse has been deprecated and replaced with argparser.You must change self.OptionParser.add_option to self.arg_parser.add_argument; the arguments are similar. parser = self.OptionParser Traceback (most recent call last): File "tikz_export.py", line 1478, in main_inkscape() File "tikz_export.py", line 1461, in main_inkscape effect = TikZPathExporter(inkscape_mode=True) File "tikz_export.py", line 629, in init self._set_up_options() File "tikz_export.py", line 646, in _set_up_options parser.set_defaults(codeoutput='standalone', crop=False, clipboard=False, AttributeError: 'TikZPathExporter' object has no attribute 'set_defaults'

  • Cylindrical coordinates plot
    by Bakkune on April 6, 2020 at 2:19 am

    I wonder how can I plot this 3D object by using only tikz-3dplot:

  • Positioning tikz arrows to coordinates inside tabulars
    by Marc Kees on August 22, 2019 at 8:40 pm

    I need to create a slide with multiple tabulars and use arrow to point to elements inside those tabulars. However, I'm having trouble to specify where tabulars, texts and arrows should appear. How can I do this? My minimal non-working example follows: \documentclass{beamer} % only frames \usepackage{tikz} \usetikzlibrary{shapes.geometric,positioning,matrix} \newcommand{\tikzmark}[1]{\tikz[remember picture] \node[coordinate] (#1) {#1};} \begin{document} \begin{frame}\frametitle{Example} \begin{tabular}{ccccc} \tikzmark{col1} \begin{tabular}{|c|}\hline \tikzmark{elem1}2\\\hline 3\\\hline 7\\\hline 6\\\hline 1\\\hline 5\\\hline 4\\\hline \end{tabular}& \tikzmark{col2} \begin{tabular}{|c|}\hline \tikzmark{elem2}4\\\hline 2\\\hline 1\\\hline 3\\\hline 6\\\hline 7\\\hline 5\\\hline \end{tabular}& \tikzmark{mat} \begin{tabular}{|c|c|c|c|}\hline 1&0&1&0\\\hline 1&0&0&1\\\hline 0&1&0&1\\\hline 0&1&0&1\\\hline 0&1&0&1\\\hline 1&0&1&0\\\hline 1&0&1&0\\\hline \end{tabular}& \tikzmark{sigMat} \begin{tabular}{|c|c|c|c|}\hline \tikzmark{elem3}2&1&2&1\\\hline 2&1&4&1\\\hline 1&2&1&2\\\hline \end{tabular} \end{tabular} \begin{tikzpicture}[remember picture] \node [above=.01cm of col2] {\tiny Text Above columns}; \node[above=.01cm of mat] {\tiny Above 1st matrix}; \node[above=.1cm of sigMat]{\tiny Above 2nd matrix}; \node[above=2cm of mat,text width=6cm] (txt1) {Arrows go from here to...}; \path[draw,->] (txt1.south west) -- node {goes to 1st elem} (elem1); \path[draw,->] (txt1.south) -- node {goes to 2nd elem} (elem2); \path[draw,->] (txt1.south east) -- node {goes to 3rd elem} (elem3); \end{tikzpicture} \end{frame} \end{document} This results in the following slide:

  • Drawing a perspective ellipse with TikZ
    by Dinesh Shankar on December 12, 2018 at 7:21 pm

    I made the following ellipse in Inkscape But I would like to draw it within TikZ environment.My final goal is to put this ellipse inside a coordinate system. Using the image from Inkscape, here is my try \documentclass[tikz,border=3.14mm]{standalone} \usepackage{tikz-3dplot} \usetikzlibrary{3d,shapes.geometric,shadows.blur} \usepackage{graphicx} % small fix for canvas is xy plane at z % https://tex.stackexchange.com/a/48776/121799 \makeatletter \tikzoption{canvas is xy plane at z}[]{% \def\tikz@plane@origin{\pgfpointxyz{0}{0}{#1}}% \def\tikz@plane@x{\pgfpointxyz{1}{0}{#1}}% \def\tikz@plane@y{\pgfpointxyz{0}{1}{#1}}% \tikz@canvas@is@plane} \makeatother \begin{document} \tdplotsetmaincoords{60}{130} \begin{tikzpicture}[scale=3.2,tdplot_main_coords,>=latex,line join=bevel] \coordinate (O) at (0,0,0); \draw[thick,->] (O) -- (1.2,0,0) node[anchor=north east]{$x$}; \draw[thick,->] (O) -- (0,1.2,0) node[anchor=north west]{$y$}; \draw[thick,->] (O) -- (0,0,1.2) node[anchor=south]{$z$}; \draw[dashed] (O) -- (-1.2,0,0); \pgfmathsetmacro{\rvec}{1.5} \pgfmathsetmacro{\thetavec}{40} \pgfmathsetmacro{\phivec}{60} \tdplotsetcoord{P}{\rvec}{\thetavec}{\phivec} \node[anchor=south west,color=red] at (P) {$B$}; \draw[-stealth,color=red,very thick] (O) -- (P); \begin{scope}[canvas is yz plane at x=0] \node[] (elliL) at (0,0) {\includegraphics[width=.1\textwidth]{image.eps}}; \end{scope} \end{tikzpicture} \end{document} Using Tikz my best attempt was \documentclass[tikz,border=3.14mm]{standalone} \usepackage{tikz-3dplot} \usetikzlibrary{3d,shapes.geometric,shadows.blur} \usepackage{graphicx} % small fix for canvas is xy plane at z % https://tex.stackexchange.com/a/48776/121799 \makeatletter \tikzoption{canvas is xy plane at z}[]{% \def\tikz@plane@origin{\pgfpointxyz{0}{0}{#1}}% \def\tikz@plane@x{\pgfpointxyz{1}{0}{#1}}% \def\tikz@plane@y{\pgfpointxyz{0}{1}{#1}}% \tikz@canvas@is@plane} \makeatother \begin{document} \tdplotsetmaincoords{60}{130} \begin{tikzpicture}[scale=3.2,tdplot_main_coords,>=latex,line join=bevel] \coordinate (O) at (0,0,0); \draw[thick,->] (O) -- (1.2,0,0) node[anchor=north east]{$x$}; \draw[thick,->] (O) -- (0,1.2,0) node[anchor=north west]{$y$}; \draw[thick,->] (O) -- (0,0,1.2) node[anchor=south]{$z$}; \draw[dashed] (O) -- (-1.2,0,0); \pgfmathsetmacro{\rvec}{1.5} \pgfmathsetmacro{\thetavec}{40} \pgfmathsetmacro{\phivec}{60} \tdplotsetcoord{P}{\rvec}{\thetavec}{\phivec} \node[anchor=south west,color=red] at (P) {}; \draw[-stealth,color=red,very thick] (O) -- (P); \begin{scope}[canvas is xz plane at y=0] \node[ellipse,fill=gray,fill opacity=0.3,draw,minimum width=2cm,minimum height=1cm,rotate=90] (elliL) at (0,0) {}; \end{scope} \end{tikzpicture} \end{document}

  • PGFPlots fillbetween layer issue with main layer
    by bz-mof on April 6, 2017 at 9:02 pm

    I am using fill between to create some diagrams where I need semi-transparent fillings above ploted lines. As normally fillings are put into a separate layer pre main below plot lines, I tried to move the fillings via on layer=main. They disappear? I don't know if this behaviour is intended. I can draw them on any other layer but this one. See the minimal example below: \documentclass[]{article} \usepackage{pgfplots} \pgfplotsset{compat=1.14} \usepgfplotslibrary{fillbetween} \begin{document} \begin{tikzpicture} \begin{axis} \addplot+[name path=A,domain=0:1,samples=10] {x}; \addplot+[name path=B,domain=0:1, samples=10] {x^2}; \addplot fill between[of=A and B, on layer=main]; \end{axis} \end{tikzpicture} \end{document} Result My workaround at the moment is to draw everything onto the pre main layer. Edit: Giving the option in the \addplot part of the statement has absolutely no effect: \documentclass[]{article} \usepackage{pgfplots} \pgfplotsset{compat=1.14} \usepgfplotslibrary{fillbetween} \begin{document} \begin{tikzpicture} \begin{axis}[domain=0:1] \addplot+[name path=A, draw=none, mark=none] {x}; \addplot+[name path=B, draw=none, mark=none] {x^2}; \addplot+[draw=black, mark=none] {x-0.1}; \addplot [on layer=axis foreground, fill=green] fill between[of=A and B]; \addplot+[draw=blue, mark=none] {x-0.15}; \end{axis} \end{tikzpicture} \end{document} Result

  • How to draw a 2D vector field in pgfplots?
    by ILoveJesus on January 26, 2017 at 8:43 pm

    I'm pretty new to LaTeX and I need to draw a 2D vector field (Like a graph with vector arrows all over it. I can't find it anywhere online. I'd like to use pgfplots if that's possible.

  • Using \pause inside a tikzpicture environment in a beamer presentation
    by Onil90 on July 14, 2016 at 2:20 pm

    I am preparing a presentation using beamer. I would like to show a picture written using tikz in two steps. This is my first attempt: \documentclass{beamer} \usepackage{tikz} \usetikzlibrary{calc} \usetikzlibrary{arrows} \usetikzlibrary{decorations.markings} \usetheme{Berlin} \begin{document} \begin{frame} \begin{center} \begin{tikzpicture} \draw[gray, thick] (-3,2) -- (3,2); \draw[gray, thick, dotted] (-3.5, 2) -- (-3,2); \draw[gray, thick, dotted] (3, 2) -- (3.5,2); \draw[gray, thick] (-3,0) -- (3,0); \draw[gray, thick, dotted] (-3.5, 0) -- (-3,0); \draw[gray, thick, dotted] (3, 0) -- (3.5,0); \draw[gray, thick] (-2,1) ellipse (0.6 and 1); \draw[gray, thick] (-1,1) ellipse (0.6 and 1); \draw[gray, thick] (0,1) ellipse (0.6 and 1); \draw[gray, thick] (-2.5,1) ellipse (0.6 and 1); \draw[gray, thick] (-1.5,1) ellipse (0.6 and 1); \draw[gray, thick] (-0.5,1) ellipse (0.6 and 1); \draw[red, very thick] (0.5,1) ellipse (0.6 and 1); \draw[gray, thick] (1,1) ellipse (0.6 and 1); \draw[gray, thick] (1.5,1) ellipse (0.6 and 1); \draw[gray, thick] (2,1) ellipse (0.6 and 1); \draw[gray, thick] (2.5,1) ellipse (0.6 and 1); \draw (3.5, 1) node [anchor = west]{$M$}; \pause \draw (0.5, 0) node [anchor = north, red]{$N$}; \draw[red, very thick] (-3.3,1.7) -- (2.6,1.7); \draw[red, very thick, dotted] (-3.8, 1.7) -- (-3.3,1.7); \draw[red, very thick, dotted] (2.6, 1.7) -- (3.1,1.7); \end{tikzpicture} \end{center} \end{frame} \end{document} I.e. I've just used the command \pause inside the tikzpicture environment. Unfortunately this result is quite unsatisfactory because the blue strip on the bottom (produced by the theme Berlin) does not appear immediately, but at the second step. How could I solve my problem? Any help will be really appreciated. Thank you very much!

  • Shaded 3D ellipsoid with pgfplot
    by Tyrion on August 13, 2015 at 12:01 pm

    I'm trying to display a shaded 3D ellipsoid with axis labels with PGFplots. So far I achieved this. \documentclass{standalone} \usepackage{tikz} \usepackage{pgfplots,tikz-3dplot} \begin{document} \begin{tikzpicture} \begin{axis}[% width=0.8\textwidth, axis equal, axis lines = center, y label style={at={(axis cs:0,2,0)},anchor=west}, xlabel = {$\lambda_1,\ \vec{e}_1$}, ylabel = {$\lambda_2,\ \vec{e}_2$}, zlabel = {$\lambda_3,\ \vec{e}_3$}, ticks=none, ] \addplot3[% shader=interp, opacity = 0.3, fill opacity=0.3, surf, colormap/blackwhite, variable = \u, variable y = \v, domain = 0:180, y domain = 0:360, ] ({2*cos(u)*sin(v)}, {2*sin(u)*sin(v)}, {0.5*cos(v)}); \end{axis} \end{tikzpicture} \end{document} The result looks like this: This shading does not give a 3D impression, I would like to have somethin like this: Is it possible to define a new 3d shading in this coordinate system? I could not find such a command in the pgfplot documentation. All predefined shadings are not suitable. Glad for any help!

  • Drawing a Cube with Tikz/PgfPlots Looks Stretched along X-Axis
    by Rüdiger on October 10, 2014 at 10:38 am

    I'm trying to draw a cubic structure (fcc crystal structure) with Tikz. I'm using pgfplots's axis cs function for the 3D projection. But the result looks like a rectangle stretched along the x axis. Here is the result: Here is the code: \documentclass{article} \usepackage{tikz} \usetikzlibrary{calc,arrows,positioning} \usepackage{pgfplots} \pgfplotsset{compat=1.9} \begin{document} \begin{tikzpicture}[ vector/.style={thick,black,>=stealth,->}, atom/.style={blue} ] \begin{axis}[ axis lines=none, xmin=-0.1, xmax=1.1, ymin=-0.1, ymax=1.1, zmin=-0.1, zmax=1.1, xtick=\empty, ytick=\empty, ztick=\empty ] \coordinate (A0) at (axis cs:0,0,0); \coordinate (A1) at (axis cs:1,0,0); \coordinate (A2) at (axis cs:0,1,0); \coordinate (A3) at (axis cs:0,0,1); \coordinate (A4) at (axis cs:1,1,0); \coordinate (A5) at (axis cs:1,0,1); \coordinate (A6) at (axis cs:0,1,1); \coordinate (A7) at (axis cs:1,1,1); \coordinate (A8) at (axis cs:0.5,0.5,0); \coordinate (A9) at (axis cs:0.5,0,0.5); \coordinate (A10) at (axis cs:0,0.5,0.5); \coordinate (A11) at (axis cs:0.5,0.5,1); \coordinate (A12) at (axis cs:0.5,1,0.5); \coordinate (A13) at (axis cs:1,0.5,0.5); \draw[black!25] (A0) -- (A8) -- (A4); \draw[black!25] (A4) -- (A12) -- (A6); \draw[black!25] (A0) -- (A10) -- (A6); \draw[black!25] (A1) -- (A8) -- (A2); \draw[black!25] (A2) -- (A10) -- (A3); \draw[black!25] (A7) -- (A12) -- (A2); \draw[black] (A1) -- (A13) -- (A7); \draw[black] (A3) -- (A11) -- (A7); \draw[black] (A5) -- (A13) -- (A4); \draw[black] (A6) -- (A11) -- (A5); \draw[black] (A5) -- (A9) -- (A0); \draw[black] (A0) -- (A1); \draw[black] (A0) -- (A2); \draw[black] (A0) -- (A3); \draw[black] (A0) -- (A3); \draw[black] (A3) -- (A5); \draw[black] (A3) -- (A6); \draw[black] (A6) -- (A2); \draw[black] (A5) -- (A1); \draw[black] (A7) -- (A4); \draw[black] (A5) -- (A7); \draw[black] (A1) -- (A4); \draw[black] (A6) -- (A7); \draw[black] (A2) -- (A4); \fill[blue] (A8) circle (5pt); \draw[vector] (A0) -- (A8); \fill[blue] (A10) circle (5pt); \draw[vector] (A0) -- (A10); \draw[black] (A3) -- (A9) -- (A1); \fill[atom] (A1) circle (5pt); \fill[atom] (A2) circle (5pt); \fill[atom] (A3) circle (5pt); \fill[atom] (A4) circle (5pt); \fill[atom] (A5) circle (5pt); \fill[atom] (A6) circle (5pt); \fill[atom] (A7) circle (5pt); \fill[atom] (A9) circle (5pt); \fill[atom] (A11) circle (5pt); \fill[atom] (A12) circle (5pt); \fill[atom] (A13) circle (5pt); \draw[vector] (A0) -- (A9); \fill[blue] (A0) circle (5pt); \end{axis} \end{tikzpicture} \end{document} What causes the stretching? How can I change it into a perfect cube?

  • pgfplots 3d cylindrical coordinate system
    by Markus on March 31, 2014 at 3:16 am

    I am trying to plot a cylindrical coordinate system using pgfplots. The idea would be to plot the r,theta plane similar to what polaraxis provides, see here. I don't know how to translate something like this into a 3D environment. Is there an easy way to plot a custom coordinate system within the current 3D axis? UPDATE: I may not have been precise enough. I am specifically interested in plotting the coordinate/axis system, i.e. the r,theta grid in a 3D plot.

  • Draw in cylindrical and spherical coordinates
    by Kiyoshi on February 9, 2014 at 10:41 am

    I can try to draw this in TikZ: I managed to draw the coordinate axis. The first image is in cylindrical coordinates and the second in spherical coordinates. I don't know draw in spherical coordinate system, the arrow labels, curved lines, and many other things. I have started to read the manual of Till Tantau, but for now I'm a newbie with TikZ and I don't understand many things of this manual.

  • Draw realistic 3D crystal structures (diamond)
    by strpeter on October 29, 2013 at 10:44 pm

    I want to draw a realistic 3D crystal structure, e.g. diamond. The point where I struggle is to draw connecting 3D rods. Desired result In the end it should describe the cubic cell of diamond, e.g. like the following picture from Phase Transformations & Complex Properties Research Group. Comments To me it seemed not reasonable at all to use TikZ as the picture gets useless at different view angles as I recognized by asking the question Unordered layers in tikz-3dplot (3d effect). Even the example in question Help creating solid cylinders along a path in Tikz for crystal lattice diagram using asymptote has unrealistic 3D layers (i.e. all atoms in front of the connecting rods - and the atoms are semitransparent in contrast to the image presented there). Example Here is a pstricks example: \documentclass{standalone} \usepackage{pstricks} \usepackage{pst-solides3d} \begin{document} \begin{pspicture}(-4,-4)(14,14) \psSolid[object=cylindre, h=6, r=1, fillcolor=yellow](0,0,0) \psSolid[object=sphere, r=2, fillcolor=red!25](0,0,0) \psSolid[object=sphere, r=2, fillcolor=red!25](0,0,6) \end{pspicture} \end{document} Result:

  • Help creating solid cylinders along a path in Tikz for crystal lattice diagram
    by shane on May 17, 2013 at 4:55 pm

    I am trying to draw a diagram using Tikz that will show the crystalline lattice structure of diamond. It should look like the following illustration (reference). I have managed to figure out the locations of each of the carbon atoms. I defined each of those locations as a coordinate (A, B, ... Q) and I managed to draw the atoms, but I don't like the look of the lines that show which atoms are connected. My problem is that the lines are not distinct enough from one another. Instead of plain lines I would like to draw three dimensional cylinders that connect the atoms together as I think that would make the image look much better. I have no idea how to get Tikz to do this, however. Here's what I've got so far. Anybody have an idea? \documentclass[10pt]{article} \usepackage{tikz} \begin{document} \pagestyle{empty} \begin{tikzpicture}[scale = 1] %points on cube \coordinate (A) at (0,0,0); \coordinate (B) at (0,0,4); \coordinate (D) at (0,4,0); \coordinate (C) at (0,4,4); \coordinate (E) at (4,0,0); \coordinate (F) at (4,0,4); \coordinate (H) at (4,4,0); \coordinate (G) at (4,4,4); %center of faces \coordinate (I) at (0,2,2); %center of face ABCD \coordinate (J) at (4,2,2); %center of face EFGH \coordinate (K) at (2,4,2); %center of face DCGH \coordinate (L) at (2,0,2); %center of face ABFE \coordinate (M) at (2,2,4); %center of face CBGF \coordinate (N) at (2,2,0); %center of face DAEH %connectors \coordinate (O) at (1,1,3); \coordinate (P) at (1,3,1); \coordinate (Q) at (3,1,1); \coordinate (R) at (3,3,3); %place non-atom cube corners \shadedraw [ball color= gray] (A) circle (0.1cm); \shadedraw [ball color= gray] (C) circle (0.1cm); \shadedraw [ball color= gray] (F) circle (0.1cm); \shadedraw [ball color= gray] (H) circle (0.1cm); \shadedraw [ball color= black] (B) circle (0.25cm); \shadedraw [ball color= black] (D) circle (0.25cm); \shadedraw [ball color= black] (E) circle (0.25cm); \shadedraw [ball color= black] (G) circle (0.25cm); %draw the center of each face \shadedraw [ball color= red] (I) circle (0.25cm); \shadedraw [ball color= red] (J) circle (0.25cm); \shadedraw [ball color= red] (K) circle (0.25cm); \shadedraw [ball color= red] (L) circle (0.25cm); \shadedraw [ball color= red] (M) circle (0.25cm); \shadedraw [ball color= red] (N) circle (0.25cm); %connectors \shadedraw [ball color= blue] (O) circle (0.25cm); \shadedraw [ball color= blue] (P) circle (0.25cm); \shadedraw [ball color= blue] (Q) circle (0.25cm); \shadedraw [ball color= blue] (R) circle (0.25cm); %draw cube \draw [dashed] (A) -- (B); \draw [dashed] (B) -- (C); \draw [dashed] (C) -- (D); \draw [dashed] (D) -- (A); \draw [dashed] (E) -- (F); \draw [dashed] (F) -- (G); \draw [dashed] (G) -- (H); \draw [dashed] (H) -- (E); \draw [dashed] (A) -- (E); \draw [dashed] (B) -- (F); \draw [dashed] (C) -- (G); \draw [dashed] (D) -- (H); %connections from faces to O \draw [very thick] (B) -- (O); \draw [very thick] (I) -- (O); \draw [very thick] (M) -- (O); \draw [very thick] (L) -- (O); %connections from faces to P \draw [very thick] (N) -- (P); \draw [very thick] (I) -- (P); \draw [very thick] (D) -- (P); \draw [very thick] (K) -- (P); %connections from faces to Q \draw [very thick] (E) -- (Q); \draw [very thick] (J) -- (Q); \draw [very thick] (N) -- (Q); \draw [very thick] (L) -- (Q); %connections from faces to R \draw [very thick] (G) -- (R); \draw [very thick] (M) -- (R); \draw [very thick] (J) -- (R); \draw [very thick] (K) -- (R); \end{tikzpicture} \end{document}

  • Putting text in a box: custom box : different color for each line
    by user16609 on March 28, 2013 at 11:40 pm

    For a beamer presentation, I would like to enclose text in a box such that: - all the line of the box are blank, - except the upper left corner, and the lower right corner of the box which are color X. \begin{\mybox}{blue} Some text here \end{\mybox} (I made this example by hand, the text doesn't have to be centered). I saw some examples where the box lines all have the same color. But I don't see how to adapt them.

  • Drawing 3D crystal lattice with tikz/pstricks
    by Iohann on February 7, 2013 at 9:56 pm

    I need to draw 3D lattice of DO3 type (Ni-Mn-Al, DO3) and more complex ones. Both TiKZ and PSTricks packages draw following objects on top of previously drawn disregarding "real" 3D position. For example, on this picture 2 red atoms on the right cover white ones, when they shouldn't. \documentclass[tikz]{standalone} \begin{document} \begin{tikzpicture}[->] \draw (0,0) -- (4,0,0); \draw (0,0) -- (0,4,0); \draw (0,0) -- (0,0,4); \foreach \x in {1,2,3} \foreach \y in {1,2,3} \foreach \z in {1,2,3} \draw (\x,\y,\z) circle (2pt); \foreach \x in {1.5,2.5} \foreach \y in {1.5,2.5} \foreach \z in {1,2,3} \draw[gray,very thin] (\x,\y,\z) +(-.5,-.5) rectangle ++(.5,.5); \foreach \x in {1,2,3} \draw (\x,1,1) -- (\x,1,3) (\x,2,1) -- (\x,2,3) (\x,3,1) -- (\x,3,3) ; \foreach \x in {1.5,2.5} \foreach \y in {1.5,2.5} \foreach \z in {1.5,2.5} \filldraw[red] (\x,\y,\z) circle (2pt); \end{tikzpicture} \end{document} It is possible, to obtain right behavior with layers or certain order of the drawing. However for complex lattices it is very difficult and depends on view angle.

  • How can I draw a vector field (quiver plot) on a torus in 3D?
    by Håkon Marthinsen on June 21, 2012 at 2:34 pm

    I'd like to draw a vector field on a torus using PGFPlots and/or TikZ. I managed to do it using ePiX, based on the file decorate.xp from the example gallery, but then I have to do z-sorting by hand, which is very ugly. How can I get PGFPlots and/or TikZ to give the same result? It seems to me that PGFPlots can plot the vector field using quiver, and plot the torus as a surf, but not both at once. Is it possible to get around this by chopping up the drawing into smaller pieces? How should I do that? NB: The code is a hack, and is probably badly written and formatted. Any tips for improving the figure and/or code are appreciated! /* -*-ePiX code based on on the example file decorate.xp-*- */ #include <algorithm> #include "epix.h" using namespace ePiX; const int N1(60); // latitudes const int N2(60); // longitudes const double du(2*M_PI/N1); const double dv(2*M_PI/N2); const double r_0(.999); // minor radius const double R_0(1); // major radius P F(double u, double v) { return polar(R_0 + r_0*Cos(u), v) + P(0,0,r_0*Sin(u)); } namespace ePiX { class mesh_quad { private: P pt1, pt2, pt3, pt4, center; double distance; public: mesh_quad(P f(double u, double v), double u0, double v0) : pt1(f(u0,v0)), pt2(f(u0+du,v0)), pt3(f(u0+du,v0+dv)), pt4(f(u0,v0+dv)), center(0.25*(pt1 + pt2 + pt3 + pt4)), distance(max(max(max(norm(pt1-camera.viewpt()), norm(pt2-camera.viewpt())), norm(pt3-camera.viewpt())), norm(pt4-camera.viewpt()))) { } double how_far() const { return distance; } void draw() const { P direction(center-camera.viewpt()); P normal((pt2 - pt1)*(pt4 - pt1)); normal *= 1/norm(normal); fill(Gray(normal|(recip(distance)*direction))); pen(0); quad(pt1, pt2, pt3, pt4); fill(false); } }; class mesh_vf { private: P pt1, pt2, center; double distance; public: mesh_vf(P f(double u, double v), double u0, double v0) { pt1 = f(u0,v0); double C = 2 * (pt1.x2() - 8); double Gamma = -C * Sin(u0) * Sin(v0); double Delta = C * (1 + Cos(u0)) * Cos(v0); pt2 = f(u0,v0) + .005 * P(Gamma * Sin(u0) * Cos(v0) + Delta * (1 + Cos(u0)) * Sin(v0), Gamma * Sin(u0) * Sin(v0) - Delta * (1 + Cos(u0)) * Cos(v0), -Gamma * Cos(u0)); center = .5*(pt1 + pt2); distance = min(norm(pt1-camera.viewpt()),norm(pt2-camera.viewpt())); //distance = norm(center-camera.viewpt()); } double how_far() const { return distance; } void draw() const { plain(Black()); arrow(pt1, pt2, .3); } }; class by_distance { public: bool operator() (const mesh_quad& arg1, const mesh_quad& arg2) { return arg1.how_far() > arg2.how_far(); } bool operator() (const mesh_vf& arg1, const mesh_vf& arg2) { return arg1.how_far() > arg2.how_far(); } bool operator() (const mesh_quad& arg1, const mesh_vf& arg2) { return arg1.how_far() > arg2.how_far(); } bool operator() (const mesh_vf& arg1, const mesh_quad& arg2) { return arg1.how_far() > arg2.how_far(); } }; } // end of namespace int main() { picture(P(-2.5,-3.5),P(3.5,3), "12x10cm"); begin(); set_crop(); viewpoint(sph(10, 80 * M_PI / 180, 30 * M_PI / 180)); // background grids pen(Gray(.5)); grid(P(-2, -2, -2), P(2, 2, -2), 4,4); grid(P(-2, -2, -2), P(2, -2, 2), 4,4); // n.b. (z,x) divisions grid(P(-2, -2, -2), P(-2, 2, 2), 4,4); label(P(2, 0, -2), P(-15, -15), "$y$", bl); label(P(2, -2, -2), P(-2, -2), "$-2$", bl); label(P(2, -1, -2), P(-2, -2), "$-1$", bl); label(P(2, 0, -2), P(-2, -2), "$0$", bl); label(P(2, 1, -2), P(-2, -2), "$1$", bl); label(P(-2, 2, 0), P(15, -15), "$z$", br); label(P(-2, 2, -1), P(2, 0), "$-1$", br); label(P(-2, 2, 0), P(2, 0), "$0$", br); label(P(-2, 2, 1), P(2, 0), "$1$", br); label(P(-2, 2, 2), P(2, 0), "$2$", br); label(P(0, 2, -2), P(0, -20), "$x$", b); label(P(-1, 2, -2), P(0, -3), "$-1$", b); label(P(-2, 2, -2), P(0, -3), "$-2$", b); label(P(0, 2, -2), P(0, -3), "$0$", b); label(P(1, 2, -2), P(0, -3), "$1$", b); label(P(2, 2, -2), P(0, -3), "$2$", b); // build and draw a torus with vector field std::vector<mesh_quad> mesh; for (int i=0; i<N1; ++i) for (int j=0; j<N2; ++j) mesh.push_back(mesh_quad(F, i*du, j*dv)); sort(mesh.begin(), mesh.end(), by_distance()); std::vector<mesh_vf> vf; for (int i=0; i<N1; ++i) for (int j=0; j<N2; ++j) vf.push_back(mesh_vf(F, i*du, j*dv)); // vf.push_back(mesh_vf(F, (i+.5)*du, (j+.5)*dv)); sort(vf.begin(), vf.end(), by_distance()); int j = 0; for (unsigned int i=0; i<mesh.size(); ++i) { mesh.at(i).draw(); if (j < vf.size()) { for ( ; (vf.at(j).how_far() > mesh.at(i).how_far()) && (j < vf.size()); j++) vf.at(j).draw(); } } for ( ; j < vf.size(); j++) vf.at(j).draw(); //label_color(Red()); //spot(P(0,2,0)); tikz_format(); end(); } Here is a screenshot of the result:

  • TikZ Drawing a changing vector field in a grid
    by Nils on June 6, 2011 at 12:21 pm

    So far I succeeded in drawing a simple grid along with it's centres using grid and nodes. \draw[step=1] (0, 0) grid (4, 3); \coordinate (a) at (0.4, 0.3); \coordinate (b) at (3.8, 0.8); \draw[fill=white,thick,->] (a) -- (b); \foreach \x in {0.5, 1.5, 2.5, 3.5} { \foreach \y in {0.5, 1.5, 2.5} { \node at (\x, \y)[circle, fill=black, scale=0.25] {}; } } However the nodes representing the centroids of the cells, I would also like to add a vector field. The vectors in a column should point in the same direction, but along the x axis they should point more in the upward direction as one goes to the right side (like Pi/8, Pi/4, 3*Pi/4, etc). It's clear to me how this would be programmed in a "normal" programming language, but I don't see how this fits into TikZ's foreach loop.. Also I'm wondering weather it's possible to have a curvilinear grid instead of a simple rectangular.