For stock, you just hold number of stock, not principal and gain separately.
You bought 100 stocks with $100. (investmemt = $10,000)
Now it is $200. (total value = $20,000)
If you sell 50 stocks for $200, you would have $10,000 on your hand.
Then, capital gain is ($200 – $100) * 50 = $5,000.
You need to pay tax for this gain of $5,000.
Tax happens only when you sell.