TikZ-PGF – Automatic Setup of Imaginary Unit in Tick Marks for Imaginary Axis

axispgfplotstickstikz-pgf

I have seen solutions like this and this (in the question section), but they don't use the axis subenvironment in the tikzpicture environment.

If I make a 2-D plot, the ticks, tick marks, and tick spacings appear based on how one sets up the settings, but only "real" numbers appear (if one wants to set up the tick marks such that all the tick marks have π in each of them). I don't mind doing that since π is a real constant.

In the case of the imaginary unit i (or j in the engineering world), I would like it to appear automatically in the tick values on the imaginary axis.

Here is the MWE for functional purposes:

\documentclass[12pt]{article}
\usepackage{pgfplots}
\usepackage{float}
\pgfplotsset{compat=1.17}
\usepackage{tikz}
\usetikzlibrary{shapes, arrows.meta, automata, positioning, matrix, calc}
\usepackage[RPvoltages, american,siunitx]{circuitikz}
\usepackage[margin=1in]{geometry} 
\tikzset{block/.style={draw, rectangle, 
        minimum height=3em, minimum width=3em},
    sum/.style={draw, circle, node distance=1cm},
    input/.style={coordinate},
    output/.style={coordinate},
    pinstyle/.style={pin edge={to-,thin,black}}}
\begin{document}    
    \begin{figure}[H]\centering
        \begin{tikzpicture}
            \begin{axis}[
                xmin=-20,xmax=5,
                ymin=-5,ymax=5,
%               ytick={-10, -5, 5, 10}, %when axis equal is set
%               yticklabels={$-j10$, $-j5$,  $j5$, $j10$},
                ytick={-4, -2, 2, 4}, %when axis equal is not set
                yticklabels={$-j4$, $-j2$,  $j2$, $j4$},
                axis lines=center,
                axis line style=->, xlabel = {$\sigma$}, ylabel={$j\omega$},
                %axis equal,
                every axis y label/.style={at={(ticklabel* cs:1.05)},anchor=south},
                every axis x label/.style={at={(ticklabel* cs:1.05)},anchor=west},
                legend cell align={left},
                legend pos=outer north east]
            \end{axis}
        \end{tikzpicture}
    \end{figure}
\end{document}

Output (for axis equal not set):

enter image description here

Is there a way to make a setting in the axis subenvironment in tikzpicture such that when the vertical ticks automatically adjust due to setting changes, the j will automatically appear to the left of the number (and to the right of the minus sign if the number is negative), or to the right of the number?

Best Answer

\documentclass[border=1 cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}    
\begin{tikzpicture}
\begin{axis}[
xmin=-20, xmax=5,
ymin=-5, ymax=5,
axis lines=center,
xlabel = {$\sigma$}, ylabel={$j\omega$},
every axis y label/.style={at={(ticklabel* cs:1.05)},anchor=south},
every axis x label/.style={at={(ticklabel* cs:1.05)},anchor=west},
yticklabel={$\pgfmathparse{\tick > 0 ? "j" : "-j"} \pgfmathresult \pgfmathparse{abs(\tick)} {\pgfmathprintnumber{\pgfmathresult}}$},
%yticklabel={$\pgfmathprintnumber{\tick} j$}, %This is for j to appear on the right side of the number instead of the left
]
\end{axis}
\end{tikzpicture}
\end{document}

Graph with custom y ticks