Forgot password
 Register account
View 1639|Reply 9

行内公式的可换行 left right 尝试

[Copy link]

673

Threads

110K

Posts

218

Reputation

Show all posts

kuing posted 2020-10-17 15:12 |Read mode
代码及测试:
\documentclass{article}
\usepackage[papersize={12cm,9cm},margin=2cm]{geometry}

\makeatletter
\newdimen\Ht
\newcommand\khhlr[4]{% khhlr = 可换行 left right
\setbox0=\hbox{$\m@th#3#4$}\Ht=\ht0\advance\Ht by\dp0%
\mathopen{\hbox{$\left#1\rule[-\dp0]{0pt}{\Ht}\right.\n@space$}}%参考了 source2e
#4%
\mathclose{\hbox{$\left.\rule[-\dp0]{0pt}{\Ht}\right#2\n@space$}}%
}
\newcommand\khh[2]{\mathpalette{\khhlr#1#2}}
\makeatother

\newcommand\khhp{\khh()}
\newcommand\khhb{\khh[]}
\newcommand\khhB{\khh\{\}}
\newcommand\khhv{\khh||}

\begin{document}

$\khhp{x}+\khhb{x^2}+\khhB{\frac12}+\khhv{\sum}+\khh[){0,\sqrt{\sum}}$

\[\khhp{x}+\khhb{x^2}+\khhB{\frac12}+\khhv{\sum}+\khh[){0,\sqrt{\sum}}\]

text text text text text text $\displaystyle\khhB{\frac12+\frac12+\frac12+\frac12+\frac12+\frac12}$
text text text text text text $\displaystyle\khh(]{\frac12+\frac12+\frac12+\frac12+\frac12+\frac12}$

\end{document}
效果:
QQ截图20201017151049.png

673

Threads

110K

Posts

218

Reputation

Show all posts

original poster kuing posted 2020-10-17 15:43
原理其实很简单,就是先将内容存入 box0 里,然后获取它的高度深度,据此分别打出左括号、内容、右括号,它们不是一个整体,所以在行内的时候就能够自动换行(注意不是人工换行,\\ 是无效的)。
其实 big, Big 之类也是一样道理,只不过那些是固定的括号,而我所多做的就是获取内容的高度信息来确定括号。
既然如此,那为啥 latex 的设计者没弄这样的东东?难道有啥副作用,又或者这种做法有点浪费资源?我不清楚……

414

Threads

1641

Posts

15

Reputation

Show all posts

abababa posted 2020-10-17 16:55
回复 1# kuing

谢谢,这个已经解决了我刚才发那帖的问题。

我刚才编译了一下,有些地方感觉还是没有正确换行,比如这个:
  1. \begin{theorem}[拉格朗日中值定理]
  2. 设函数$f(x)$是区间$[a,b]$上的连续函数,集合$S = \myset{x \in [a,b]: \text{在$x \in (a,b)$使得$f(x)$可导}}$.
  3. \end{theorem}
Copy the Code
定理.png

这个如果改成:“$f(x)$是定义在区间$[a,b]$上的连续函数”,加了几个字,它还是从冒号那里折行,而如果少几个字,这一行的字间距感觉就有点拉开了。

673

Threads

110K

Posts

218

Reputation

Show all posts

original poster kuing posted 2020-10-17 17:03
回复 3# abababa
这是因为你的 \text{在 ... 可导} 把这句话变成一个整体了,它就无法在这里分开。
改成这样试试
\begin{theorem}[拉格朗日中值定理] 
设函数$f(x)$是区间$[a,b]$上的连续函数,集合$S = 
\myset{x \in [a,b]: \text{在}~x \in (a,b)~\text{使得}~f(x)~\text{可导}}$. 
\end{theorem}

673

Threads

110K

Posts

218

Reputation

Show all posts

original poster kuing posted 2020-10-17 17:08
回复 4# kuing

其实我也不知道公式里的 ~ 会是咋处理,如果不行的话,把 ~ 改成 \空格 再试

不过就算 ~ 或 \空格 都不能换行,至少在 \in 那里可以换

414

Threads

1641

Posts

15

Reputation

Show all posts

abababa posted 2020-10-17 17:13
回复 4# kuing

原来如此,原来是text弄的没有换行。
觉得这种公式里又有汉字又有公式的挺麻烦,就用text括起来,然后里面再打上公式,一次就成了,不用那么多text,不知道正规的做法是不是这样。

414

Threads

1641

Posts

15

Reputation

Show all posts

abababa posted 2025-4-20 21:19
主楼的那个在下标里,会变得大一些,例如\bigcup_{x\in(A\setminus\khhB{1})},这时它在下标里,随行模式时不太正常,我改成下面这样:
  1. \makeatletter
  2. \newdimen\Ht
  3. \newcommand\khhlr[4]{% 可换行 left right
  4.         \setbox0=\hbox{$\m@th#3#4$}\Ht=\ht0\advance\Ht by\dp0%
  5.         \mathopen{%
  6.                 \mathchoice
  7.                         {\hbox{$\m@th\left#1\rule[-\dp0]{0pt}{\Ht}\right.$}}
  8.                         {\hbox{$\m@th\left#1\rule[-\dp0]{0pt}{\Ht}\right.$}}
  9.                         {\{\vphantom{#4}}
  10.                         {\{\vphantom{#4}}
  11.         }%
  12.         #4%
  13.         \mathclose{%
  14.                 \mathchoice
  15.                         {\hbox{$\m@th\left.\rule[-\dp0]{0pt}{\Ht}\right#2$}}
  16.                         {\hbox{$\m@th\left.\rule[-\dp0]{0pt}{\Ht}\right#2$}}
  17.                         {\vphantom{#4}\}}
  18.                         {\vphantom{#4}\}}
  19.         }%
  20. }
  21. \newcommand\khh[2]{\mathpalette{\khhlr#1#2}}
  22. \makeatother
Copy the Code

觉得这个在下标里会好一点。

673

Threads

110K

Posts

218

Reputation

Show all posts

original poster kuing posted 2025-4-20 22:02
abababa 发表于 2025-4-20 21:19
主楼的那个在下标里,会变得大一些,例如\bigcup_{x\in(A\setminus\khhB{1})},这时它在下标里,随行模式时 ...
不用改得这么复杂(粗略看下也改得不对),出问题的原因只是我 1# 有一个小疏忽:
存放内容的 hbox 里有 #3(这个参数是视情况代入 displaystyle, textstyle 等的),而两括号的 hbox 却忘了加 #3。
所以只要在那两个 \left 前面补回一个 #3 即可。

测试代码:
  1. \documentclass{article}
  2. \usepackage[papersize={12cm,12cm},margin=2cm]{geometry}
  3. \makeatletter
  4. \newdimen\Ht
  5. \newcommand\khhlr[4]{% khhlr = 可换行 left right
  6. \setbox0=\hbox{$\m@th#3#4$}\Ht=\ht0\advance\Ht by\dp0%
  7. \mathopen{\hbox{$#3\left#1\rule[-\dp0]{0pt}{\Ht}\right.\n@space$}}%参考了 source2e
  8. #4%
  9. \mathclose{\hbox{$#3\left.\rule[-\dp0]{0pt}{\Ht}\right#2\n@space$}}%
  10. }
  11. \newcommand\khh[2]{\mathpalette{\khhlr#1#2}}
  12. \makeatother
  13. \newcommand\khhp{\khh()}
  14. \newcommand\khhb{\khh[]}
  15. \newcommand\khhB{\khh\{\}}
  16. \newcommand\khhv{\khh||}
  17. \begin{document}
  18. $\khhp{x}+\khhb{x^2}+\khhB{\frac12}+\khhv{\sum}+\khh[){0,\sqrt{\sum}}$
  19. \[\khhp{x}+\khhb{x^2}+\khhB{\frac12}+\khhv{\sum}+\khh[){0,\sqrt{\sum}}\]
  20. text text text text text text $\displaystyle\khhB{\frac12+\frac12+\frac12+\frac12+\frac12+\frac12}$
  21. text text text text text text $\displaystyle\khh(]{\frac12+\frac12+\frac12+\frac12+\frac12+\frac12}$
  22. \[\bigcup_{x\in(A\setminus\khhB{1})}\]
  23. \end{document}
Copy the Code

673

Threads

110K

Posts

218

Reputation

Show all posts

original poster kuing posted 2025-4-20 22:31
PS、\mathpalette 就是展开成 \mathchoice 的, 1# / 8# 的展开过程是这样的:
\khhp{x}
-> \khh(){x}
-> \mathpalette{\khhlr()}{x}
-> \mathchoice
	{\khhlr()\displaystyle{x}}
	{\khhlr()\textstyle{x}}
	{\khhlr()\scriptstyle{x}}
	{\khhlr()\scriptscriptstyle{x}}}
所以 \khhlr 有四个参数,第三个就是 style。

Comment

原来如此😀。  posted 2025-4-21 15:05

Quick Reply

Advanced Mode
B Color Image Link Quote Code Smilies
You have to log in before you can reply Login | Register account

$\LaTeX$ formula tutorial

Mobile version

2025-7-15 14:32 GMT+8

Powered by Discuz!

Processed in 0.021626 seconds, 35 queries