1

Sorry for the basic question. I'm practicing how to use circuitikz, meanwhile I want to show the process of code and output. For the elegant of LaTeX source code in pdf what I want to do is to ignore some indentations or not to display indentation symbols in showexpl LTXexample, It affects the experience of viewing so more.

Here is MWE. It would be nice if the two scenarios could be realized separately(ignore or not to display indentation).

\documentclass{article}
\usepackage{listings} %插入代码块
\usepackage{tikz}
\usepackage{circuitikz}
\usepackage{showexpl} 
\lstset{%
    basicstyle=\ttfamily\small,
    commentstyle=\itshape\ttfamily\small,
    showspaces=false,
    showstringspaces=false,
    showtabs=false,
    breaklines=true,
    backgroundcolor=\color{gray!10!white},
    breakautoindent=false,
    captionpos=b,
}

\begin{document} \begin{LTXexample}[pos=r,preset=\centering,width=0.5\linewidth,rangeaccept=false,title=potentiometer] \begin{circuitikz} \draw (0,0) to [potentiometer, name=P, mirror] ++(0,2); \draw (P.wiper) to[L] ++(2,0); \end{circuitikz} \end{LTXexample} \end{document}

LTXexample output

I want to ignore or not to display the content of red boxes

  • Sorry, I cannot reproduce your screenshot with the shown code. So my answer (see below) may or may not help. – cabohah Mar 10 '24 at 11:17

2 Answers2

1

I don't get your output with ^^I, but just an indent:

enter image description here

Even if I replace the spaces by tabulators, I don't see ^^I^^I, but

using tab

The listings provides option gobble to ignore a number of characters at the beginning of lines. So you can for example use (note, the example uses spaces not tabular):

\documentclass{article}
\usepackage{listings}
\usepackage{tikz}
\usepackage{circuitikz}
\usepackage{showexpl} 
\lstset{%
    basicstyle=\ttfamily\small,
    commentstyle=\itshape\ttfamily\small,
    showspaces=false,
    showstringspaces=false,
    showtabs=false,
    breaklines=true,
    backgroundcolor=\color{gray!10!white},
    breakautoindent=false,
    captionpos=b,
}

\begin{document} \lstset{gobble=4} \begin{LTXexample}[pos=r,preset=\centering,width=0.5\linewidth,rangeaccept=false,title=potentiometer]
\begin{circuitikz} \draw (0,0) to [potentiometer, name=P, mirror] ++(0,2); \draw (P.wiper) to[L] ++(2,0); \end{circuitikz} \end{LTXexample} \end{document}

to get

reduce indent

If you are using tabulators you can at least use \lstset{tabsize=1} to reduce the indent. Unfortunately listings does only allow numbers > 0. So \lstet{tabsize=0} to remove the whole indent would result in an error.

But you can define a new command to activate ignoring of all tabulators:

\makeatletter
\NewCommandCopy\lst@doProcessTabulator\lst@ProcessTabulator
\newcommand*{\lstignoretab}{%
  \let\lst@ProcessTabulator\relax
}
\newcommand*{\lstshowtab}{%
  \DeclareCommandCopy\lst@ProcessTabulator\lst@doProcessTabulator
}
\makeatother

Now you can switch between ignoring tabulators using \lstignoretab to get

ignore tabulators

and not ignoring them using \lstshowtab to get, (with default tabsize=8):

using tab

cabohah
  • 11,455
  • @DavidCarlisle With tabs I also cannot reproduce the shown image with ^^I but get indents like in my last image. I'm using an up-to-date TeX Live 2023. – cabohah Mar 10 '24 at 11:52
  • My computer system is Windows11 and TEX version is—TeX 3.141592653 (TeX Live 2023),kpathsea version 6.3.5. Just now, after looking at your answers, I retested on the online site overleaf(https://www.overleaf.com/), it also did not reproduce the previous result ^^I . Maybe the reason of output code in pdf contain ^^I is because of the local tex version? – Jason Haynes Mar 10 '24 at 12:34
  • I retested in local tex version, I agree with you more,I delete spaces in latex source code ,then replace it in tabulators according tap tab keys in keyboard,then,I didn't find ^^I happen again in pdf. – Jason Haynes Mar 10 '24 at 12:47
  • David, thank you very much, Cheers! – Jason Haynes Mar 10 '24 at 12:59
0

Welcome!

As a reference, this is how it's done (more or less) in the circuitikz manual. I added the colored background for the code (nice touch).For the white space at the start, you may check How to remove the leading unnecessary white spaces when using linerange option?

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{circuitikz}
%
% Thanks to Ulrike Fischer https://tex.stackexchange.com/a/57160/38080
% make the listing line number invisible to copy and paste
% it seems that sometimes it works, sometimes no!
%
\RequirePackage{accsupp}
\newcommand{\emptyaccsupp}[1]{\BeginAccSupp{ActualText={}}#1\EndAccSupp{}}%
%
\RequirePackage{showexpl}
\lstset{frameround=fttt}
\lstloadlanguages{TeX}
\lstset{explpreset={pos=l,
    width=-99pt,
    overhang=0pt,
    hsep=\columnsep,
    vsep=\bigskipamount,
    rframe=single,
    xleftmargin=1em,
    columns=flexible,
    language=[LaTeX]TEX,
    breaklines=true,
    basicstyle=\small\ttfamily,
    numbers=left,
    numbersep=.3em,
    numberstyle=\tiny\emptyaccsupp, 
    tabsize=3,
    backgroundcolor=\color{red!10!white}}}
\begin{document}
\begin{LTXexample}[pos=r,preset=\centering,width=0.5\linewidth,rangeaccept=false,title=potentiometer]
\begin{circuitikz}
    \draw (0,0) to [potentiometer, name=P, mirror] ++(0,2);
    \draw (P.wiper) to[L] ++(2,0);
\end{circuitikz}
\end{LTXexample}
\end{document}

enter image description here

Rmano
  • 40,848
  • 3
  • 64
  • 125