Forgot password?
 Create new account
View 165|Reply 6

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

[Copy link]

3147

Threads

8493

Posts

610K

Credits

Credits
66163
QQ

Show all posts

hbghlyj Posted at 2024-2-27 04:09:54 |Read mode
直引號'在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}
Copy the Code

效果:
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}
Copy the Code
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.

3147

Threads

8493

Posts

610K

Credits

Credits
66163
QQ

Show all posts

 Author| hbghlyj Posted at 2024-2-27 04:40:22
可以避免設新的變量,把\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}
Copy the Code

這裡的39是用`查到的:
  1. \documentclass{article}
  2. \begin{document}
  3. \number\numexpr`'
  4. \end{document}
Copy the Code

701

Threads

110K

Posts

910K

Credits

Credits
94177
QQ

Show all posts

kuing Posted at 2024-2-27 14:27:49
  1. \number\numexpr`'
Copy the Code

第一次见😃这招好

701

Threads

110K

Posts

910K

Credits

Credits
94177
QQ

Show all posts

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

3147

Threads

8493

Posts

610K

Credits

Credits
66163
QQ

Show all posts

 Author| hbghlyj Posted at 2024-2-27 17:20:55
kuing 发表于 2024-2-27 09:01
其实在英文文本中经常有纯单引号 's 之类的使用
apostrophe可以用 \textquotesingle
texlive.net/run?%5Cdocumentclass%7Barticle%7D … %5Cend%7Bdocument%7D
6oCaG[1].png
  1. \documentclass{article}
  2. \begin{document}
  3.     `It\textquotesingle s a nice day!'
  4. \end{document}
Copy the Code

701

Threads

110K

Posts

910K

Credits

Credits
94177
QQ

Show all posts

kuing Posted at 2024-2-28 18:26:14
  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}
Copy the Code

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

3147

Threads

8493

Posts

610K

Credits

Credits
66163
QQ

Show all posts

 Author| hbghlyj Posted at 2024-2-28 18:50: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}
Copy the Code

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

手机版Mobile version|Leisure Math Forum

2025-4-20 22:20 GMT+8

Powered by Discuz!

× Quick Reply To Top Return to the list