找回密码
 快速注册
搜索
查看: 47|回复: 7

使'交替輸出左引號、右引號

[复制链接]

3149

主题

8386

回帖

6万

积分

$\style{scale:11;fill:#eff}꩜$

积分
65391
QQ

显示全部楼层

hbghlyj 发表于 2024-2-27 04:09 |阅读模式
直引號'在LaTeX中預設是右引號。在LaTeX中可以修改它,使它交替輸出左引號、右引號
  1. \documentclass{article}
  2. \newcount\quoteCount
  3. \quoteCount=0
  4. \let\rightquote='
  5. \catcode`'=13 % Make ' an active character
  6. \def'{%
  7.     \ifodd\quoteCount
  8.         \rightquote%
  9.     \else
  10.         `%
  11.     \fi
  12.     \advance\quoteCount by 1
  13. }
  14. \begin{document}
  15. Hello, 'world'!
  16. Hello, 'world'!
  17. \end{document}
复制代码

效果:
Hello, ‘world’!
Hello, ‘world’!

上面用的是TeX counts,或者可以用LaTeX counter,它們的區別:texdev.net/2009/11/17/tex-counts-and-latex-counters/
需把\newcounter\quoteCount改為\newcounter{quoteCount},把\iffodd\quoteCount改為\iffodd\value{quoteCount},把\advance\quoteCount by 1改為\stepcounter{quoteCount}:
  1. \documentclass{article}
  2. \newcounter{quoteCount}
  3. \let\rightquote='
  4. \catcode`'=13 % Make ' an active character
  5. \def'{%
  6.     \ifodd\value{quoteCount}%
  7.         \rightquote%
  8.     \else
  9.         `%
  10.     \fi
  11.     \stepcounter{quoteCount}%
  12. }
  13. \begin{document}
  14. Hello, 'world'!
  15. Hello, 'world'!
  16. \end{document}
复制代码
Why choice one or other method? Well, LaTeX does error checking and also adds some refinements (such as resetting one counter based on another). It also ensures you always include the appropriate \relax statements to avoid TeX picking up ‘extra’ material for numbers. On the other hand, if you want to do local assignments then you have to use TeX’s count registers: LaTeX always does global assignments. I tend to find that for document-level things counters are best (anything I actually want to print), whereas count registers are more flexible for programming.

3149

主题

8386

回帖

6万

积分

$\style{scale:11;fill:#eff}꩜$

积分
65391
QQ

显示全部楼层

 楼主| hbghlyj 发表于 2024-2-27 04:40
可以避免設新的變量,把\let\rightquote='去除,舊的\rightquote改为\char39:
  1. \documentclass{article}
  2. \newcount\quoteCount
  3. \quoteCount=0
  4. \catcode`'=13 % Make ' an active character
  5. \def'{%
  6.     \ifodd\quoteCount
  7.         \char39%
  8.     \else
  9.         `%
  10.     \fi
  11.     \advance\quoteCount by 1
  12. }
  13. \begin{document}
  14. Hello, 'world'!
  15. Hello, 'world'!
  16. \end{document}
复制代码

這裡的39是用`查到的:
  1. \documentclass{article}
  2. \begin{document}
  3. \number\numexpr`'
  4. \end{document}
复制代码

730

主题

1万

回帖

9万

积分

积分
93593
QQ

显示全部楼层

kuing 发表于 2024-2-27 14:27
  1. \number\numexpr`'
复制代码

第一次见😃这招好

730

主题

1万

回帖

9万

积分

积分
93593
QQ

显示全部楼层

kuing 发表于 2024-2-27 17:01
其实在英文文本中经常有纯单引号 's 之类的使用,所以使 ' 交替输出左右引号其实是不合适的。
这帖的意义纯粹是学习这种定义方式而已。

3149

主题

8386

回帖

6万

积分

$\style{scale:11;fill:#eff}꩜$

积分
65391
QQ

显示全部楼层

 楼主| hbghlyj 发表于 2024-2-27 17:20
kuing 发表于 2024-2-27 09:01
其实在英文文本中经常有纯单引号 's 之类的使用

apostrophe可以用 \textquotesingle
texlive.net/run?%5Cdocumentclass%7Barticle%7D%0A%5Cbegin%7Bdocum ... 0s%20a%20nice%20day!
6oCaG[1].png
  1. \documentclass{article}
  2. \begin{document}
  3.     `It\textquotesingle s a nice day!'
  4. \end{document}
复制代码

730

主题

1万

回帖

9万

积分

积分
93593
QQ

显示全部楼层

kuing 发表于 2024-2-28 18:26
  1. \documentclass{article}
  2. \catcode`'=13 % Make ' an active character
  3. \def'{\lq\let\xxq\lq\let\lq\rq\let\rq\xxq}
  4. \begin{document}
  5. Hello, 'world'!
  6. Hello, 'world'!
  7. Hello, 'world'!
  8. I'm fine!
  9. Hello, 'world'!
  10. \end{document}
复制代码

效果:
Hello, ‘world’!
Hello, ‘world’!
Hello, ‘world’!
I‘m fine!
Hello, ’world‘!

3149

主题

8386

回帖

6万

积分

$\style{scale:11;fill:#eff}꩜$

积分
65391
QQ

显示全部楼层

 楼主| hbghlyj 发表于 2024-2-28 18:50
  1. \documentclass{article}
  2. \catcode`'=13 % Make ' an active character
  3. \def\lq{\char96\let'\rq}
  4. \def\rq{\char39\let'\lq}
  5. \let'\lq
  6. \begin{document}
  7. Hello, 'world'!
  8. Hello, 'world'!
  9. \end{document}
复制代码

Hello, ‘world’!
Hello, ‘world’!

3149

主题

8386

回帖

6万

积分

$\style{scale:11;fill:#eff}꩜$

积分
65391
QQ

显示全部楼层

 楼主| hbghlyj 发表于 2025-2-27 01:03
  1. \usepackage[autostyle]{csquotes}
  2. \MakeOuterQuote{"}
复制代码

手机版|悠闲数学娱乐论坛(第3版)

GMT+8, 2025-3-4 07:30

Powered by Discuz!

× 快速回复 返回顶部 返回列表