我正在尝试构建一个宏来输出内容tcolorbox
。最终,任何类型的内容都可以放在该框中(图像、tcolorbox、代码等)。
但是,当使用宏(而不是直接构造框)时,换行符不再出现,虽然它是相同的 tcolorbox 选项。
我读过另一个答案关闭宏存在问题,这就是我\scantokens
在宏中添加的原因。
这是 MWE:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{xparse}
\usepackage[all]{tcolorbox}
\usemintedstyle{default}% colorful, pastie
\setminted{
tabsize=2,
fontsize=\footnotesize,
fontfamily=tt,
autogobble=true,
breaklines=true,
}
\tcbset{snippetstyle/.style={
listing only,
listing engine=minted,
enhanced jigsaw,
breakable,
colback=black!20,
colframe=blue,
sharp corners,
size=small,
autoparskip,
parbox=false,
}
}
\DeclareTCBListing{codeBloc}{m}{%
snippetstyle,
minted language=#1,
minted options={numbersep=10pt,numbers=left},
}%
\DeclareTotalTColorBox{\sameBoxInMacro}{ +m }{enhanced jigsaw,title=JAVA application}
{
\scantokens{%
#1
}
}
\begin{document}
\section{Macro used to build the tcolorbox}
Wrong behavior : no newlines
\sameBoxInMacro{%
This is a sample code
\begin{codeBloc}{Python}
import socket
def client(string):
HOST, PORT = 'localhost', 2000
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((HOST, PORT))
sock.send(string)
reply = sock.recv(16384) # limit reply to 16K
sock.close()
return reply
assert client('2+2') == '4'
\end{codeBloc}
}%
\section{tcolorbox directly build without macro}
\begin{tcolorbox}[enhanced jigsaw,title=JAVA application]
This is a sample code
\begin{codeBloc}{Python}
import socket
def client(string):
HOST, PORT = 'localhost', 2000
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((HOST, PORT))
sock.send(string)
reply = sock.recv(16384) # limit reply to 16K
sock.close()
return reply
assert client('2+2') == '4'
\end{codeBloc}
\end{tcolorbox}
\end{document}
其输出如下:
虽然这两个框具有相同的定义,但第一个是通过宏调用的,\sameBoxInMacro
而后者是直接通过构建的\begin{tcolorbox}
。
我怎样才能使我构建的宏尊重内容中的换行符?