设想
我想写一个关于 PSTricks 的教程,但是因为latex-dvips-ps2pdf
(参见第一次尝试部分中显示的代码)我阻止读者复制列表编号的努力不再起作用
\usepackage{accsupp}
\newcommand*{\noaccsupp}[1]{\BeginAccSupp{ActualText={}}#1\EndAccSupp{}}
我不能使用microtype
,输出有点奇怪,如下所示
结果,这个问题迫使我使用pdflatex
(参见第二次尝试部分)。
第一次尝试(latex-dvips-ps2pdf)
\documentclass[dvipsnames,border=12pt,preview]{standalone}
\usepackage{pstricks}
\usepackage{showexpl}
\usepackage{accsupp}
\newcommand*{\noaccsupp}[1]{\BeginAccSupp{ActualText={}}#1\EndAccSupp{}}
\makeatletter
\newlength{\parindent@save}
\setlength{\parindent@save}{\parindent}
\lstdefinestyle{PSTricks}
{
language={[LaTeX]TeX},
alsolanguage={PSTricks},
basicstyle=\small\ttfamily\null,
keywordstyle=\color{blue},
backgroundcolor=\color{yellow!10},
numbers=left,
numbersep=1em,
numberstyle=\tiny\color{Red}\noaccsupp,
frame=single,
framesep=\fboxsep,% expands outward, cannot affect if frame=none
framerule=\fboxrule,% expands outward, cannot affect if frame=none
rulecolor=\color{red},% cannot affect if frame=none
xleftmargin=\dimexpr\fboxsep+\fboxrule,
xrightmargin=\dimexpr\fboxsep+\fboxrule,
breaklines=true,
breakindent=0pt,
tabsize=5,
columns=flexible,
preset={\setlength{\parindent}{\parindent@save}\centering},
}
\makeatother
\begin{document}
\LTXexample[pos=t,style=PSTricks]
\begin{pspicture}[showgrid](3,3)
\pscircle[linecolor=red](1.5,1.5){1.5}
\end{pspicture}
\endLTXexample
\end{document}
第二次尝试 (pdflatex)
使用pdflatex
可以防止用户复制列表编号。但是,preset=\centering
当我使用时,不再起作用graphic.
\documentclass[dvipsnames,border=12pt,preview]{standalone}
\usepackage{xcolor}
\usepackage{showexpl}
\usepackage{accsupp}
\newcommand*{\noaccsupp}[1]{\BeginAccSupp{ActualText={}}#1\EndAccSupp{}}
\makeatletter
\newlength{\parindent@save}
\setlength{\parindent@save}{\parindent}
\lstdefinestyle{PSTricks}
{
language={[LaTeX]TeX},
alsolanguage={PSTricks},
basicstyle=\small\ttfamily\null,
keywordstyle=\color{blue},
backgroundcolor=\color{yellow!10},
numbers=left,
numbersep=1em,
numberstyle=\tiny\color{Red}\noaccsupp,
frame=single,
framesep=\fboxsep,% expands outward, cannot affect if frame=none
framerule=\fboxrule,% expands outward, cannot affect if frame=none
rulecolor=\color{red},% cannot affect if frame=none
xleftmargin=\dimexpr\fboxsep+\fboxrule,
xrightmargin=\dimexpr\fboxsep+\fboxrule,
breaklines=true,
breakindent=0pt,
tabsize=5,
columns=flexible,
preset={\setlength{\parindent}{\parindent@save}\centering},
}
\makeatother
\usepackage{filecontents}
\begin{filecontents*}{diagram.tex}
\documentclass[pstricks,border=12pt]{standalone}
\begin{document}
\begin{pspicture}[showgrid](3,3)
\pscircle[linecolor=red](1.5,1.5){1.5}
\end{pspicture}
\end{document}
\end{filecontents*}
\immediate\write18{latex diagram.tex && dvips diagram.dvi && ps2pdf diagram.ps}
\begin{document}
\LTXinputExample[pos=t,style=PSTricks,graphic={[scale=1]"diagram"}]{diagram.tex}
\end{document}
原始输出:
预期输出:
问题
\LTXexample
如何使第二次尝试部分显示的代码的图形居中?
答案1
尝试
...
\makeatletter
\let\SX@I\SX@resultInput
\def\SX@resultInput{\centerline{\SX@I}}
\makeatother
\begin{document}
\LTXinputExample[pos=t,style=PSTricks,graphic={[scale=1]"diagram"}]{diagram.tex}
\end{document}
答案2
取自Herbert 的另一个答案(点击)几十年前我就用过它,我修改过以满足我的需求。但说实话,我不确定它是否适合长期使用。
\documentclass[dvipsnames,border=12pt,preview]{standalone}
\usepackage{xcolor}
\usepackage{showexpl}
\usepackage{accsupp}
\newcommand*{\noaccsupp}[1]{\BeginAccSupp{ActualText={}}#1\EndAccSupp{}}
\makeatletter
\renewcommand*\SX@resultInput{%
\ifx\SX@graphicname\@empty
\begingroup
\MakePercentComment\makeatother\catcode`\^^M=5\relax
\SX@@preset\SX@preset
\if@SX@rangeaccept
\let\SX@tempa=\SX@input
\else
\let\SX@tempa=\input
\fi
\SX@tempa{\SX@codefile}\par%
\endgroup
\else
\sbox\SX@tempbox{\includegraphics{\SX@graphicname}}
\begin{center}
\expandafter\includegraphics\expandafter[\SX@graphicparam]{\SX@graphicname}
\end{center}
\fi
}
\newlength{\parindent@save}
\setlength{\parindent@save}{\parindent}
\lstdefinestyle{PSTricks}
{
language={[LaTeX]TeX},
alsolanguage={PSTricks},
basicstyle=\small\ttfamily\null,
keywordstyle=\color{blue},
backgroundcolor=\color{yellow!10},
numbers=left,
numbersep=1em,
numberstyle=\tiny\color{Red}\noaccsupp,
frame=single,
framesep=\fboxsep,% expands outward, cannot affect if frame=none
framerule=\fboxrule,% expands outward, cannot affect if frame=none
rulecolor=\color{red},% cannot affect if frame=none
xleftmargin=\dimexpr\fboxsep+\fboxrule,
xrightmargin=\dimexpr\fboxsep+\fboxrule,
breaklines=true,
breakindent=0pt,
tabsize=5,
columns=flexible,
preset={\setlength{\parindent}{\parindent@save}},
}
\makeatother
\usepackage{filecontents}
\begin{filecontents*}{diagram.tex}
\documentclass[pstricks,border=12pt]{standalone}
\begin{document}
\begin{pspicture}[showgrid](3,3)
\pscircle[linecolor=red](1.5,1.5){1.5}
\end{pspicture}
\end{document}
\end{filecontents*}
\immediate\write18{latex diagram.tex && dvips diagram.dvi && ps2pdf diagram.ps}
\begin{document}
\LTXinputExample[pos=t,style=PSTricks,graphic={[scale=1]"diagram"}]{diagram.tex}
\LTXexample[pos=b]
\LaTeX\ is fun!
\endLTXexample
\end{document}